Data | Web




    Python Workshop 4

    A smooth sea never made a skilled sailor.

    - John George Hermanson




    1. In a small town in country Dreamland, the initial population is 1000 at the beginning of a year. The population regularly increases by 4 percent per year and recently the town has set up new industries. So,100 new inhabitants come for job or studies every year.



    The townhead wants an estimate that in how many years, the the town will get certain population based on varying 'percentage of population growth' and 'number of incoming people'.

    A sample question from townhead would be:

    How many years does the town need to see its target population greater or equal to 1200 inhabitants?

    At the end of the first year there will be: 
    1000 + 1000 * 0.04 + 100 => 1140 inhabitants

    At the end of the 2nd year there will be: 
    1140 + 1140 * 0.04 + 100 =>  1286 inhabitants (population can not be fraction)

    It will need 2 entire years.

    More generally given parameters:

    population(initial population), percent, newcomers, target (population to surpass)

    the function number_of_years should return n number of entire years needed to get a population greater or equal to target.

    newcomers is an integer, percent a positive or null number, population and target are positive integers (> 0)

    number_of_years(1500, 5, 100, 10000) -> 26
    number_of_years(1500000, 2.5, 10000, 2000000) -> 10


    2. Some languages like Chinese, Japanese & Thai do not have spaces between words. However, most natural languages processing tasks like part-of-speech tagging require texts that have segmented words. 

    "MaxMatch" is a simple and reasonably effective algorithm to segment a sentence into its component words.

    MaxMatch starts at the first character of a sentence and tries to find the longest valid word starting from that character. If no word is found, the first character is deemed the longest "word", regardless of its validity. In order to find the rest of the words, MaxMatch is then recursively invoked on all of the remaining characters until no characters remain. A list of all of the words that were found is returned.

    So for the string "happynight", "happy" is found because "happynight" is not a valid word, nor is "happyni", nor "happyn". Then, MaxMatch is called on "night", and "night" is found. The output is the list ["happy", "night"] in that order.

    The Challenge
    Write max_match, which takes an alphanumeric, spaceless, lowercased String as input and returns a List of Strings of all the words/ alphabets found, in the order they were found. All valid words including alphabets can be obtained with below set of code:

    import nltk'words')
    english_vocab = set(w.lower() for w in nltk.corpus.words.words())

    english_vocab will contain all the valid words & alphabets. 




    ['happy', 'night', 'zig']




    ['happy', 'night', 'z', 'i']


    3.  An array is circularly sorted if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps.

    Complete the function/method that determines if the given array of integers is circularly sorted.

    These arrays are circularly sorted (true):

    [2, 3, 4, 5, 0, 1]          -->       [0, 1] + [2, 3, 4, 5]
    [4, 5, 6, 9, 1]              -->       [1] + [4, 5, 6, 9]
    [10, 11, 6, 7, 9]          -->       [6, 7, 9] + [10, 11]
    [1, 2, 3, 4, 5]              -->       [1, 2, 3, 4, 5]
    [5, 7, 43, 987, -9, 0]  -->       [-9, 0] + [5, 7, 43, 987]

    While these are not (false):

    [4, 1, 2, 5]
    [8, 7, 6, 5, 4, 3]
    [6, 7, 4, 8]
    [7, 6, 5, 4, 3, 2, 1]


    4. The new movie "SpaceXtreme" has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollar bill. An "SpaceXtreme" ticket costs 25 dollars.


    Steve is currently working as a clerk. He wants to sell a ticket to every single person in this line.

    Can Steve sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue?

    Return YES, if Steve can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return NO.


    tickets([25, 25, 50]) # => YES 
    tickets([25, 100]) # => NO. Steve will not have enough money to give change to 100 dollars
    tickets([25, 25, 50, 50, 100]) # => NO. Steve will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)


    5. Given a String of length N reverse each word in it. Words are separated by comma.

    The first line contains T denoting the number of testcases. Then follows description of testcases. Each case contains a string containing dots and characters.


    For each test case, output a String in single line containing the reversed words of the given String.

    1<=Length of String<=2000





    6. Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

    For example:

     persistence(38) => 2  # Because 3*8 = 24, 2*4 = 8 and 8 has only one digit.

     persistence(999) => 4 # Because 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12, and finally 1*2 = 2.

     persistence(4) => 0   # Because 4 is already a one-digit number.


    7. Maria needs to get on a train that leaves from the station D kilometres away in T minutes. She can get a taxi that drives at S1 km/h for the price of R €/km or she can walk at S2 km/h for free.



    A correct solution will be a function that returns the minimum price she needs to pay the taxi driver or the string "Won't make it!".
    All the inputs will be positive integers, the output has to be a string containing a number with two decimal places - or "Won't make it!" if that is the case.

    It won't take her any time to get on the taxi or the train.

    In non-trivial cases, the need is to combine walking and riding the taxi so that she makes it, but pays as little as possible.

    The input parameters will be in that order:

    Distance, Time, Taxi speed, Price rate of taxi per km, Walking speed


    calculate_optimal_fare(10, 50, 30, 10, 6)

    Output: 62.50


    calculate_optimal_fare(100, 50, 30, 10, 6)

    Output: "Won't make it!"





    Possible Solutions:

    1. Colab notebook 

    2. Colab notebook

    3. Colab notebook

    4. Colab notebook

    5. Colab notebook

    6. Colab notebook

    7. Colab notebook




    testuser says (Sep 12, 2019):

    Test comment

Post Comments