Programming Chalagens


  1. Posts : 22,740
    Windows 10 Home x64
       #1

    Programming Chalagens


    I've been doing these in Python but any language can be used.

    1. love6
    The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.

    love6(6, 4) → True
    love6(4, 5) → False
    love6(1, 5) → True
    Solution in Python version 2.7.9
    PHP Code:
    def love6(ab):
        if (
    == or == 6):
            return 
    True
        elif 
    (== 6):
            return 
    True
        elif 
    (abs(b) == 6):
            return 
    True
        
    else:
            return 
    False 
    Results of Test;
    Last edited by BunnyJ; 20 Apr 2015 at 19:28.
      My Computer


  2. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #2

    2. caught_speeding:
    You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.

    caught_speeding(60, False) → 0
    caught_speeding(65, False) → 1
    caught_speeding(65, True) → 0
    Solution in Python 2.7.9:
    PHP Code:
    def caught_speeding(speedis_birthday):
        
    ticket int(0)

        if 
    is_birthday:
            if 
    speed >= 66 and speed <= 85:
                
    ticket 1
            elif speed 
    >= 86:
                
    ticket 2
        
    else:
            if 
    speed >= 61 and speed <= 81:
                
    ticket 1
            elif speed 
    >= 81:
                
    ticket 2

        
    return ticket 
    Results of Test:
      My Computer


  3. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #3

    3. cigar_party..
    When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise.

    cigar_party(30, False) → False
    cigar_party(50, False) → True
    cigar_party(70, True) → True
    Solution in Python version 2.7.9:
    PHP Code:
    def cigar_party(cigarsis_weekend):

        if (
    is_weekend and cigars >= 40): return True
        
    if (cigars >= 40 and cigars <= 60): return True
        
    return False 
    Results of test:
      My Computer


  4. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #4

    4. first_half.py
    Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".

    first_half('WooHoo') → 'Woo'
    first_half('HelloThere') → 'Hello'
    first_half('abcdef') → 'abc'
    Solution in Python 2.7.9
    PHP Code:
    def first_half(str):
        
    length = (len(str) / 2)
        return 
    str[0:length
    Results:
    Programming Chalagens-capture.png
    Last edited by BunnyJ; 21 Apr 2015 at 16:54.
      My Computer


  5. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #5

    5. first_two.py
    Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "".

    first_two('Hello') → 'He'
    first_two('abcdefg') → 'ab'
    first_two('ab') → 'ab'
    Solution in Python 2.7.9:
    PHP Code:
    def first_two(str):
        return 
    str[0:2
    Results:
    Programming Chalagens-capture.png
      My Computer


  6. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #6

    6. make_tags.py
    The web is built with HTML strings like "<i>Yay</i>" which draws Yay as italic text. In this example, the "i" tag makes <i> and </i> which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word, e.g. "<i>Yay</i>".

    make_tags('i', 'Yay') → '<i>Yay</i>'
    make_tags('i', 'Hello') → '<i>Hello</i>'
    make_tags('cite', 'Yay') → '<cite>Yay</cite>'
    Solution in Python 2.7.9:
    PHP Code:
    def make_tags(tagword):
        return 
    "<{0}>{1}</{0}>".format(tag,word)

    #----------------------------------
    print make_tags('i''Yay')
    print 
    make_tags('i''Hello')
    print 
    make_tags('cite''Yay'
    Results:
    Programming Chalagens-capture.png
      My Computer


  7. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #7

    7. extra_end.py
    Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.

    extra_end('Hello') → 'lololo'
    extra_end('ab') → 'ababab'
    extra_end('Hi') → 'HiHiHi'
    Solution in Python 2.7.9:
    PHP Code:
    def extra_end(str):
        return 
    str[len(str) - 2:len(str)+1] * 3
    #-------------------------------------
    print extra_end('Hello')
    print 
    extra_end('ab')
    print 
    extra_end('Hi'
    Result:
    Programming Chalagens-capture.png
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:06.
Find Us




Windows 10 Forums