Data | Web

Shekhar

Biswas

  •  

    Python Workshop 2

    The harder you fall, the heavier your heart; the heavier your heart, the stronger you climb; the stronger you climb, the higher your pedestal.

    Criss Jami

     

     

     

    1. Count the number of occurrences of each character and return it as a list of tuples in order of appearance.

    Example:

    ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]

     

    2. Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it's invalid.

    All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.

    What is considered Valid?
    A string of braces is considered valid if all braces are matched with the correct brace.

    Examples
    "(){}[]"    =>  True
    "([{}])"    =>  True
    "(}"         =>  False
    "[(])"       =>  False
    "[({})](]"  =>  False

     

     

     

    3. A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this problem, we will restrict ourselves to decimal (base 10).

    For example, take 153 (3 digits):

        1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

    and 1634 (4 digits):

        1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634


    The Challenge:

    Your code must return true or false depending upon whether the given number is a narcissistic number in base 10.

    Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function.

     

     

    4.  A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence "The quick brown fox jumps over the lazy dog" is a pangram, because it uses the letters A-Z at least once (case is irrelevant).

    Given a string, detect whether or not it is a pangram. Return True if it is, False if not. Ignore numbers and punctuation.

     

     

    5.  In the city of Cartesia, all roads are laid out in a perfect grid. You arrived ten minutes earlier to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return True if the walk the app tells you that you will take exactly ten minutes (you don't want to be early or late!) and will, of course, return to your starting point. Return False otherwise.

    Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).

     

    ['n', 'e', 'e', 'n', 'e', 'w', 'n', 'n', 's', 'e']                                                        False
    ['e', 'n', 'w', 'e', 'e', 's', 'n', 's', 'e', 'n', 'n', 's', 'n', 'e', 'w', 'n', 'n', 'n', 's']   False
    ['s', 'e', 's', 'n', 's', 's', 's', 'w', 'e', 'e', 'e', 'w', 's']                                        False
    ['e', 'w', 'e', 'n', 's']                                                                                      False

    [s, e, w, n, n, s, e, w, n, s]                                                                          True

     

     

     

     

    Let's have a look at Possible solutions:

    1. Python-Workshop2-Q1- Soln

    2. Python-Workshop2-Q2- Soln

    3. Python-Workshop2-Q3- Soln

    4. Python-Workshop2-Q4- Soln

    5. Python-Workshop2-Q5- Soln

     

     

Comments

  • (no comments)

Post Comments