Python Test

Python Brush Up

16 cards   |   Total Attempts: 182
  

Related Topics

Cards In This Set

Front Back
Given an int count of a number of donuts, return a string of the form "Number of donuts: ', where is the number passed in. However, if the count is 10 or more, then use the word 'many' instead of actual count.
Def donuts(count): if count < 10: return 'Number of donuts: ' + str(count) else: return 'Number of donuts: many'
Given a string s, return a string made of the first 2 and the last 2 chars of the original string, so "spring' yields 'spng'. However, if the string length is less than 2, return instead the empty string.
Def both_ends(s): if len(s) < 2: return '' first2 = x[0:2] last2 = s[-2:] return first2 + last 2
Given a string s, return a string where all occurrences of its first char have been changed to '*', except do not change the first char itself. Assume string len is >= 1.
Def fix_start(s): front = s[0] back = s[1:] fixed_back = back.replace(front, '*') return front + fixed_back
Given strings a and b, return a single string with a and b separated by a space ' ' except swap the first two chars of each string. eg 'mix', 'pod' -> 'pox mid'
Def mix_up(a, b): a_swapped = b[:2] + a[2:] b_swapped = a[:2] + b[2:]return a_swapped + ' ' + b_swapped
Given a string, if its length is at least 3, add 'ing' to its end. Unless it already ends in 'ing', in which case add 'ly' instead. If the string length is less than 3, leave it unchanged.
Def verbing(s): if len(s) >=3: if s[-3:] != 'ing': s = s + 'ing' else: s = s + 'ly' return s
Given a string, find the first appearance of the substring 'not' and 'bad'. If the 'bad' follows the 'not', replace the whole 'not'...'bad' substring with 'good'.
Def not_bad(s) n = s.find('not') b = s.find('bad') if n!= -2 and b != -1 and b > n: s = s[:n] + 'good' + s[b+3:] return s
Divide a string into 2 halves. If length is even, the front and back halves are the same length. If length is odd, we'll say that the extra char goes in the fron half. i.e given 'abcde', the front half is 'abc', the back half is 'de' Given 2 strings, a and b, return a string of the form a-front + b-front + a-back + b-back
Def front_back(a,b): a_middle = len(a) /2 b_middle = len(b) /2 if len(a) % 2 ==1: a_middle = a_middle + 1 if len(b) %2 == 1: b_middle = b_middle + 1 return a[:a_middle] + b[:b_middle] + a[a_middle:] + b{b_middle:]
Write a script that allows you to pass in a name in the command line as a argument and prints out "Hello (name)!!"
Import sysdef Hello(name): print 'Hello', name, '!!'
def main(): Hello(sys.argv[1])
if __name__ == '__main__': main()
Given a list of strings, return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same.
Def match_ends(words)count = 0for word i words: if len(word) >=2 and word[0] == word[-1]: count = count + 1return count
Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with 'x' first.
Def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list)
Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple.
Def last(a): return a[-1]
def sort_las(tuples): return sorted(tuples, key = last)
Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3] returns [1,2,3].
Def remove_adjacent(numbs): result = [] for num in nums: if len(result) == 0 or num != result[-1]: result.append(num) return result
Given two lists sorted in increasing order, create and return a merged list of all the elements in sorted order.
Def linear_merge(list1, list2): result = [] while len(list1) and len(list2): if list1[0] < list2 [0]: result.append(list1.pop(0)) else: result.append(list2.pop(0)) result.extend(list1) result.extend(list2) return result
Write a program that asks two people for their names; stores the names in variables called name1 and name2; says hello to both of them.
Name1 = raw_input("What is your name?")name2 = raw_input("and what is your friend's name?")print "Well Hello", name1, "and", name2
Write a script that asks the users for the temperature in F and prints the temperature in C. (Conversion: Celsius = (F-32) * 5/9)
Temp = int(raw_input("What is the temperature in F?"))tempC = (temp - 32) * (5.0 / 9.0)print "The temperature in Celcius is",tempC