-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module3.py
177 lines (117 loc) · 4.88 KB
/
Module3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def concatenate(strings):
"""
Concatenates the given list of strings into a single string.
Returns the single string.
If the given list is empty, returns an empty string.
For example:
- If we call concatenate(["a","b","c"]), we'll get "abc" in return
- If we call concatenate([]), we'll get "" in return
Hint(s):
- Remember, you can create a single string from a list of multiple strings by using the join() function
"""
# your code here
return ''.join(strings)
def all_but_last(seq):
"""
Returns a new list containing all but the last element in the given list.
If the list is empty, returns None.
For example:
- If we call all_but_last([1,2,3,4,5]), we'll get [1,2,3,4] in return
- If we call all_but_last(["a","d",1,3,4,None]), we'll get ["a","d",1,3,4] in return
- If we call all_but_last([]), we'll get None in return
"""
# your code here
if seq!=[]:
seq1=list(seq[ :-1])
return seq1
else:
return None
def remove_duplicates(lst):
"""
Returns the given list without duplicates.
The order of the returned list doesn't matter.
For example:
- If we call remove_duplicates([1,2,1,3,4]), we'll get [1,2,3,4] in return
- If we call remove_duplicates([]), we'll get [] in return
Hint(s):
- Remember, you can create a set from a string, which will remove the duplicate elements
"""
# your code here
test_list = list(set(lst))
return test_list
def reverse_word(word):
"""
Reverses the order of the characters in the given word.
For example:
- If we call reverse_word("abcde"), we'll get "edcba" in return
- If we call reverse_word("a b c d e"), we'll get "e d c b a" in return
- If we call reverse_word("a b"), we'll get "b a" in return
- If we call reverse_word(""), we'll get "" in return
Hint(s):
- You can iterate over a word in reverse and access each character
"""
# your code here
neworder = word[::-1]
return neworder
def divisors(n):
"""
Returns a list with all divisors of the given number n.
As a reminder, a divisor is a number that evenly divides another number.
The returned list should include 1 and the given number n itself.
The order of the returned list doesn't matter.
For example:
- If we call divisors(10), we'll get [1,2,5,10] in return
- If we call divisors(1), we'll get [1] in return
"""
# your code here
lst=[]
#iterate over range from 1 to number n
for i in range(1, n + 1):
#check if i divides number x evenly to get factors
if (n % i == 0):
lst.append(i)#add them to factor list
return lst
def capitalize_or_join_words(sentence):
"""
If the given sentence starts with *, capitalizes the first and last letters of each word in the sentence,
and returns the sentence without *.
Else, joins all the words in the given sentence, separating them with a comma, and returns the result.
For example:
- If we call capitalize_or_join_words("*i love python"), we'll get "I LovE PythoN" in return.
- If we call capitalize_or_join_words("i love python"), we'll get "i,love,python" in return.
- If we call capitalize_or_join_words("i love python "), we'll get "i,love,python" in return.
Hint(s):
- The startswith() function checks whether a string starts with a particualr character
- The capitalize() function capitalizes the first letter of a string
- The upper() function converts all lowercase characters in a string to uppercase
- The join() function creates a single string from a list of multiple strings
"""
# your code here
if sentence.startswith("*"):
sentence = result = sentence.title()
result = ""
for word in sentence.split():
result += word[:-1] + word[-1].upper()+ " "
k = result[:-1]
l = k.strip("*")
m=''.join(l)
return m
else:
s = ','.join(sentence.split())
return s
def move_zero(lst):
"""
Given a list of integers, moves all non-zero numbers to the beginning of the list and
moves all zeros to the end of the list. This function returns nothing and changes the given list itself.
For example:
- After calling move_zero([0,1,0,2,0,3,0,4]), the given list should be [1,2,3,4,0,0,0,0] and the function returns nothing
- After calling move_zero([0,1,2,0,1]), the given list should be [1,2,1,0,0] and the function returns nothing
- After calling move_zero([1,2,3,4,5,6,7,8]), the given list should be [1,2,3,4,5,6,7,8] and the function returns nothing
- After calling move_zero([]), the given list should be [] and the function returns nothing
"""
# your code here
count = 0
for i in range(len(lst)):
if lst[i] != 0:
lst[i], lst[count] = lst[count], lst[i]
count += 1