Skip to content

first week homework #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
23 changes: 17 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
ends = 0
for word in words:
if len(word)>1 and word[0] == word[-1:]:
ends += 1
return ends


# B. front_x
Expand All @@ -33,8 +36,15 @@ 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
words.sort()
l = []
for word in words:
if word[0] == 'x':
l.append(word)
for word in words:
if word[0] != 'x':
l.append(word)
return l


# C. sort_last
Expand All @@ -44,8 +54,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
tuples = sorted(tuples, key=lambda tuple: tuple[-1])
#source: https://docs.python.org/3/howto/sorting.html
return tuples


# Simple provided test() function used in main() to print
Expand Down
20 changes: 16 additions & 4 deletions basic/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@
# 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
i = 0
while True:
if i+1 >= len(nums):
break
elif nums[i] == nums[i+1]:
nums.pop(i+1)
else:
i+=1
return nums


# 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
ans = []
for a in list1:
ans.append(a)
for a in list2:
ans.append(a)
ans.sort()
return ans
Copy link
Owner

Choose a reason for hiding this comment

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

A teszt ugyan atmegy igy, de a feladat kulon kiter ra, hogy linearis idoben kene mukodnie, es hogy a ket lista eleve rendezett. Python .sort() metodus O(n log n), https://en.wikipedia.org/wiki/Timsort. :)



# Note: the solution above is kind of cute, but unforunately list.pop(0)
Expand Down
16 changes: 8 additions & 8 deletions basic/string1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
# 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:
count = 'many'
return ('Number of donuts: ' + str(count))


# B. both_ends
Expand All @@ -34,8 +35,9 @@ 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 ''
return s[0] + s[1] + s[-2] + s[-1]


# C. fix_start
Expand All @@ -48,8 +50,7 @@ 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
return s[0] + s[1:].replace(s[0], '*')


# D. MixUp
Expand All @@ -60,8 +61,7 @@ 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
return b[0:2] + a[2:] + ' ' + a[0:2] + b[2:]


# Provided simple test() function used in main() to print
Expand Down
30 changes: 23 additions & 7 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:
return s
elif s[-3:] == 'ing':
return s + 'ly'
else:
return s + 'ing'


# 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
n = s.find('not')
b = s.find('bad')
if n < b:
s = s[:n] + 'good' + s[b+3:]
return s


# F. front_back
Expand All @@ -39,10 +46,19 @@ def not_bad(s):
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
# Given 2 strings, a and b, return a string of the form
# a-front + b-front + a-back + b-back
# a-front + b-front + a-back + b-back
def halve(n):
if len(n) % 2 == 0:
return len(n)/2
else:
return len(n)/2+1

def front_back(a, b):
# +++your code here+++
return
a_front = a[:halve(a)]
a_back = a[halve(a):]
b_front = b[:halve(b)]
b_back = b[halve(b):]
return a_front + b_front + a_back + b_back


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