Data | Web




    Python Workshop 3

    In many ways, it's a dull language, borrowing solid old concepts from many other languages & styles: boring syntax, unsurprising semantics, few automatic coercions, etc etc. But that's one of the things I like about Python. 

    - Tim Peters




    1. You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.

    longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) "abigailtheta"

    n being the length of the string array, if n = 0 or k > n or k <= 0 return "".

    consecutive strings : follow one after another without an interruption



    2.  Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?

    Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].

      delete_nth ([1,1,1,1],2)         # return [1,1]

      delete_nth ([20,37,20,21],1) # return [20,37,21]


    3. In this problem, given a string, replace every letter with its position in the alphabet.

    If anything in the text isn't a letter, ignore it and don't return it.

    "a" = 1, "b" = 2, etc.

    alphabet_position("The sunset sets at twelve o' clock.")
    Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" (as a string)


    4.  Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.

    ! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)

    Also, you can not use for loop to solve this,

    Examples :

    iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even

    iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd


    5. Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.


    Only lower case letters will be used (a-z). No punctuation or digits will be included.
    Performance needs to be considered
    Input strings s1 and s2 are null terminated.
    scramble('rkqodlw', 'world')                             ==> True
    scramble('cedewaraaossoqqyt', 'codewars') ==> True
    scramble('katas', 'steak')                                   ==> False


    6. The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

    maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
    # It should return that is sum of these 4 numbers [4, -1, 2, 1]
    Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

    Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.

    Hint for you



    Let's have a look at Possible Solutions

    1. Colab Notebook 

    2. Colab Notebook 

    3. Colab Notebook 

    4. Colab Notebook 

    5. Colab Notebook 

    6. Colab Notebook 



  • (no comments)

Post Comments