CSCI 1012
compressor is hard.ex1.py through ex10.py.zip with all the .py filesWrite a function ex1 that takes a tuple of ints as an argument:
ex1((4, 0)) returns int 36
ex1((2, 4, 2, 9, 0, 3)) returns int 242883
Submit as ex1.py
str(4) gives the string '4'int('4') gives the integer 4'abc' + 'def' gives 'abcdef'- Looping/iterating: T = (1,2)
for i in range(len(T)):
print('The current index is', i, '. The current element is', T[i])The current index is 0 The current element is 1
The current index is 1 The current element is 2
1
2
Write a function ex2 that takes a string and a list as arguments. Your function will return the index of the first character in the string that appears in the list. If no characters in the string appear in the list return the boolean False. Submit as ex2.py
ex2('cat', [4, 2, 'c', 'a']) returns 0
'c' appears in the list and is at index 0 in the stringex2('cat', [4, 2, 'a']) returns 1
'a' (index 1) is the first character that appears in the listex2('cat', [4, 2, 'z', 0, 'y']) returns False
'cat' appear in the list0 p
1 a
2 n
3 d
4 a
p
a
n
d
a
in to check whether an element occurs in a string/list/dictionary/etc.
's' in ['a', 'b', 's'] returns True't' in ['a', 'b', 's'] returns False's' in 'substring' returns True'a' in 'substring' returns FalseHint for Exercise 2: Loop through the string and check whether the character at each index occurs in the list.
Write a function ex3 that takes a list of strings as its argument. Return a new list that contains only the strings that are all lower-case.
ex3(['otis', 'carl', 'Diner']) returns ['otis', 'carl']ex3(['Fun', 'Cat', 'Hat', 'food']) returns ['food']ex3(['FUN', 'CAT', 'HAT']) returns []Submit as ex3.py
0 4
1 3
4
3
[1, 2, 3, 4]
[1, 2, 3] + [4] gives [1, 2, 3, 4]'LUNCH'.lower() gives the string 'lunch'DINNER
Write a function ex4 that takes a tuple as an argument and returns a dictionary where the keys are the unique elements that occured in the tuple and the values are the number of times they occurred.
ex4(('duck', 'duck', 'goose', 'teal')) returns {'duck': 2, 'goose': 1, 'teal': 1}ex4(('a', 'a', 'a', 'b')) returns {'a': 3, 'b': 1}Submit as ex4.py
94
Key Error. Use in to check whether something is a key in the dictionary first.s = "aaabbc"
counter = {}
for char in s:
if char in counter:
counter[char] += 1
else:
counter[char] = 1Hint: As on the last slide, you’ll want to similarly loop through the tuple and increment the counts in a dictionary
Write a function ex5 that takes a list of integers/floats.
'positive' if the product of all of the numbers is greater than 0'negative' if the product of all of the numbers is less than 0,'zero' if the product of all of the numbers is 0.Exempli Gratia:
ex5([1, 2, 4, 0]) returns 'zero'ex5([1, 2, -1]) returns 'negative'ex5([1, 2, 4]) returns 'positive'ex5([-1, 2, -1]) returns 'positive'Submit as ex5.py
0 4
1 3
if (CONDITION1):
# Indented code that executes if CONDITION1 is True
elif (CONDITION 2):
# Indented code that executes if CONDITION1 is False
# and CONDITION2 is True
elif (CONDITION 3):
# Indented code that executes if CONDITION1 and CONDITION2 are False
# and CONDITION 3 is True
else:
# Indented code that executes if CONDITION1, CONDITION2, CONDITION3
# are all FalseWrite a function ex6 that takes a lowercase string as its argument.
If the substring consisting of the first two characters of the string occur later alphabetically than the substring consisting of the last two characters of the string, return a new string where those substrings are swapped.
Otherwise, return the original string. Submit as ex6.py
ex6('elephant') returns 'elephant'
'el' < 'nt', so we don’t change the stringex6('telephone') returns 'nelephote'
'te' > 'ne', so we return 'ne' + 'lepho' + 'te''on'
'cn'
> and < between strings check for alphabetical order for lowercase letters.Write a function ex7 that takes a list (of strings) as its argument. If the first element of the list is later alphabetically than the last element of the list, swap them. Otherwise, keep the list the same. Return the resulting list.
ex7(['b', 'c', 'd', 'a']) returns ['a', 'c', 'd', 'b']ex7(['a', 'c', 'd', 'b']) returns ['a', 'c', 'd', 'b']ex7(['a', 'd']) returns ['a', 'd']ex7(['d', 'a']) returns ['a', 'd']Write a function ex8 that takes a string as input and returns the first character that only occurs once.
ex8('grant') returns 'g'ex8('lollipop') returns 'i'ex8('ababc') returns 'c'ex8('baaaa') returns 'b'Write a function ex9 that takes a string as input and returns the first substring where each subsequent character is later alphabetically.
ex9('abcda') returns 'abcd'ex9('ba') returns 'b'ex9('abcd') returns 'abcd'ex9('aba') returns 'ab'Write a function ex10 that reverses In-Class Exercise 4. It will take a dictionary as its argument and will return a tuple where each key in the dictionary occurs the number of times decided by the key’s value in the dictionary. The elements in the tuple can occur in any order.
ex10({'bob': 2, 'hahah': 1, 'madam': 1}) returns ('bob', 'bob', 'hahah', 'madam')ex10({‘a’: 3, ‘b’: 1}) returns (‘a’, ‘a’, ‘a’, ‘b’)