Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions basic/list1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.
def match_ends(words):
# +++your code here+++
return
count = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
count += 1
return count


# B. front_x
Expand All @@ -33,8 +36,14 @@ def match_ends(words):
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
def front_x(words):
# +++your code here+++
return
list = []
xList = []
for n in words:
if n.startswith('x'):
xList.append(n)
else:
list.append(n)
return sorted(xList) + sorted(list)


# C. sort_last
Expand All @@ -44,8 +53,9 @@ def front_x(words):
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key= function to extract the last element form each tuple.
def sort_last(tuples):
# +++your code here+++
return
def last(a):
return a[-1]
return sorted(tuples, key=last)


# Simple provided test() function used in main() to print
Expand Down
18 changes: 14 additions & 4 deletions basic/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
# +++your code here+++
return
result = []
for num in nums:
if len(result) == 0 or num != result[-1]:
result.append(num)
return result
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nagyon klassz 👍



# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution should work in "linear" time, making a single
# pass of both lists.
def linear_merge(list1, list2):
# +++your code here+++
return
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



# Note: the solution above is kind of cute, but unforunately list.pop(0)
Expand Down
24 changes: 16 additions & 8 deletions basic/string1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
# +++your code here+++
return
if count < 10:
return 'Number of donuts: ' + str(count)
else:
return 'Number of donuts: many'


# B. both_ends
Expand All @@ -34,8 +36,11 @@ def donuts(count):
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
# +++your code here+++
return
if len(s) < 2:
return ''
firstTwo = s[0:2]
lastTwo = s[-2:]
return firstTwo + lastTwo


# C. fix_start
Expand All @@ -48,8 +53,10 @@ def both_ends(s):
# Hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
def fix_start(s):
# +++your code here+++
return
front = s[0]
back = s[1:]
back_f = back.replace(front, '*')
return front + back_f


# D. MixUp
Expand All @@ -60,8 +67,9 @@ def fix_start(s):
# 'dog', 'dinner' -> 'dig donner'
# Assume a and b are length 2 or more.
def mix_up(a, b):
# +++your code here+++
return
a_changed = b[:2] + a[2:]
b_changed = a[:2] + b[2:]
return a_changed + ' ' + b_changed


# Provided simple test() function used in main() to print
Expand Down
24 changes: 18 additions & 6 deletions basic/string2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
# If the string length is less than 3, leave it unchanged.
# Return the resulting string.
def verbing(s):
# +++your code here+++
return
if len(s) >= 3:
if s[-3:] != 'ing':
s += 'ing'
else:
s += 'ly'
return s


# E. not_bad
Expand All @@ -29,8 +33,11 @@ def verbing(s):
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return
a = s.find('not')
b = s.find('bad')
if a != -1 and b != -1 and b > a:
s = s[:a] + 'good' + s[b+3:]
return s


# F. front_back
Expand All @@ -41,8 +48,13 @@ def not_bad(s):
# 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):
# +++your code here+++
return
a_kozep = len(a) / 2
b_kozep = len(b) / 2
if len(a) % 2 == 1:
a_kozep += 1
if len(b) % 2 == 1:
b_kozep += 1
return a[:a_kozep] + b[:b_kozep] + a[a_kozep:] + b[b_kozep:]


# Simple provided test() function used in main() to print
Expand Down