Sunday, January 14, 2024

 

Problem statement:

A message containing letters from A-Z can be encoded into numbers using the following mapping:

'A' -> "1"

'B' -> "2"

...

'Z' -> "26"

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:

  • "AAJF" with the grouping (1 1 10 6)
  • "KJF" with the grouping (11 10 6)

Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".

Given a string s containing only digits, return the number of ways to decode it.

The test cases are generated so that the answer fits in a 32-bit integer.

 

Example 1:

Input: s = "12"

Output: 2

Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: s = "226"

Output: 3

Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

Example 3:

Input: s = "06"

Output: 0

Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").

 

Constraints:

  • 1 <= s.length <= 100
  • s contains only digits and may contain leading zero(s).

 

class Solution {

    public int numDecodings(String s) {

        Integer count = 0;

        if (isValid(s.substring(0,1)))

            traverse(s.substring(1), count);

        if (s.length() >= 2 && isValid(s.substring(0,2)))

            traverse(s.substring(2), count);

        return count;

    }

    public boolean traverse(String s, Integer count) {

        if (String.isNullOrWhitespace(s)){

            count += 1;

            return true;

        }

        if (isValid(s.substring(0,1)))

            traverse(s.substring(1), count);

        if (s.length() >= 2 && isValid(s.substring(0,2)))

            traverse(s.substring(2), count);

        return count > 0;

    }

    public boolean isValid(String s) {

        if (s.length() == 1 && s.charAt(0) >= '0' && s.charAt(0) <= '9'){

            return true;

        }

        if (s.length() == 2 &&

           (s.charAt(0) > '0' && s.charAt(0) <= '2') &&

           ((s.charAt(0) == '1' && s.charAt(1) >= '0' && s.chartAt(1) <= '9') ||

            (s.charAt(0) == '2' && s.chartAt(1) >= '0' && s.chartAt(1) <= '6')) {

            return true;

        }

        return false;

    }

}

 

 

 

 

Saturday, January 13, 2024

 

This is a summary of the book titled “The Anger Habit” written by Carl Semmelroth and Donald E.P. Smith and published by Sourcebooks in 2000. This book aims to address the problem of habitual, uncontrollable anger. The authors argue that anger is a bad habit that can be triggered by other habits. People with an anger habit unconsciously create a mental environment that predisposes them to this emotion, interpreting others' actions and motivations to gain control. They suggest that the alternative to acting from emotional habit is acting from reason. To break the anger habit, people should learn to make conscious decisions about their reactions and respect others' right to self-determination.

Anger is destructive and can lead to loss of control and lack of reason. It can cause serious professional and personal problems, such as collisions between drivers and spouses, stifling communication, and even destroying marriages. The best solution is to replace rage with reason, as emotions can provide important signals about the angry state of mind and become sources of personal insight and information.

Anger is a powerful emotion that can be used to force others or ourselves to comply with expectations. However, expressing anger is not the best way to change situations that don't align with the plan. Instead, angry people can learn to examine their feelings of rage, gather more information, and question their expectations. They are managers of their behavior before influencing others. Anger is embedded in other habits, such as thoughts, speech, and behavior, and is situational. Unlike smoking, anger is not a sudden outburst but rather a result of thoughts, words, and actions. To reduce anxiety, it is essential to deprive the flame of its fuel, not just to stifle the heat of anger. Angry feelings contain important information, which can tell infuriated people that they have acted upon certain expectations, made observations, and arrived at certain conclusions. To stop angry outbursts, angry people must learn to question their interpretations and deprive the flame of its fuel.

Communication is often difficult when people struggle to control others, as information exchange is constantly sorted for clues of attacks. Anger can be a result of projecting discontent or unrealistic expectations onto others, leading to feelings of alienation and a fear-panic cycle. Anger often reflects frustration when trying to control others' thoughts and feelings of themselves. To overcome anger, people must examine their feelings during outbursts, which are usually unproductive and counterproductive. Self-respect is essential, not self-importance, as it is based on honest self-evaluation. The fear-panic cycle begins with chronic anger grounded in a struggle for control, producing feelings of alienation. To break this cycle, angry people must learn to turn away from seeing themselves as victims and begin to see themselves as breaking free of the burden of rage. Poor communication is also a consequence of anger, as it makes honesty impossible and creates relationships that are the opposite of honest and trusting. Anger is not just a failure to communicate but often involves force, which can reinforce fear and anger.

Angry people often seek support and encouragement from others, which can lead to misguided communication and support, which can lead to divorce or a chronic struggle for control. Anger is a habit that depends on a context, usually a power struggle, and can be reduced by facing facts and taking responsibility for one's behavior. To learn from anger, people should replace self-importance, self-contempt, and guilt with self-esteem, remorse, contest for power over others, and criticism with an open mind.

To break the anger habit, people must acknowledge the legitimate rights of others without abandoning their own values and learn to remind others of their expectations through their actions. Parents should use rules to build credibility and confidence, teaching their children to control their own behavior. When becoming parents, angry people must be aware of their own feelings and deal with their own feelings before making decisions about their children. Children's anger is similar to adults' anger, and parents must teach their children to deal with disappointment without erupting in anger.

#codingexercise https://1drv.ms/w/s!Ashlm-Nw-wnWhOZ3-qoREYBJfWBUoQ?e=f2BXqC


Friday, January 12, 2024

 This is a continuation of the listing of design choices for infrastructure consolidation that fall into well-known patterns. These include the following patterns:  

  1. Regional redundancy: This is a pattern that adds a resource or a deployment of resources in another region other than the ones used in a primary region. The paired region can be deployed on demand from backups, remain in standby in an active-passive configuration or remain operational as in an active-active configuration. This pattern is used even for full deployments of critical services for the purpose of ensuring business continuity and disaster recovery.  

  1. Managed resources/services: When a cloud resource is assigned to a team, they have all the features of the cloud to interact with it such as command-line interface, software development kit libraries, REST APIs and the cloud management User interface, but when the same resource must be shared between teams, certain operations that are allowed from the cloud must be hidden or locked and alternatives may need to be provided to support isolation between the uses. When the resource is deployed in this manner, the services become managed by one team while they are used by many other teams. 

  1. Automation of access control- Almost any resource or deployment is incomplete without access control and organizing and assigning identities is team specific. When dedicated resources are handed off, the assignee can be granted contributor access but on shared instances, appropriate group creation and role-based access control assignments become necessary even if they are far more in number than actual instances. Use of custom-roles to assign only the minimum number of permissions also become necessary. The custom-roles will have allowed and denied sets of permissions for both control plane and data plane. The exclusion of permissions in the effective permission set for a given principal guarantees the locking of that resource for consumption. 

  1. Alerting – The same best practice that applies to dedicated resources for their monitoring holds true for shared resources except that the audience is more diverse than earlier and involves different teams and their members. It is also necessary to isolate notifications specific to trouble and their intended audience, especially when many teams share the same fate on that resource. 

  1. Infrastructure layering – This pattern is evident from container infrastructure layering which allows even more scale because it virtualizes the operating system. While traditional virtual machines enable hardware virtualization and hyper V’s allow isolation plus performance, containers are cheap and barely anything more than just applications. Azure container service serves both Linux and Windows container services. It has standard docker tooling and API support with streamlined provisioning of DCOS and Docker swarm. Job based computations use larger sets of resources such as with compute pools that involve automatic scaling and regional coverage with automatic recovery of failed tasks and input/output handling. Azure demonstrates a lot of fine grained loosely coupled micro services using HTTP listener, Page content, authentication, usage analytic, order management, reporting, product inventory and customer databases.