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
20 changes: 14 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
items = 0
for i in words:
if (len(i) > 1) and (i[0] == i[-1]):
items = items + 1
return items


# 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
beginsWithX= list()
notBeginsWithX = list()
for i in words:
if i[0] == 'x':
beginsWithX.append(i)
else:
notBeginsWithX.append(i)
return sorted(beginsWithX) + sorted(notBeginsWithX)


# C. sort_last
Expand All @@ -44,8 +53,7 @@ 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
return sorted(tuples,key=lambda x: x[1])


# Simple provided test() function used in main() to print
Expand Down
26 changes: 22 additions & 4 deletions basic/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,35 @@
# 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
numsWithoutRepeating = list()
for i in nums:
if i not in numsWithoutRepeating:
numsWithoutRepeating.append(i)
return numsWithoutRepeating
Copy link
Owner

Choose a reason for hiding this comment

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

Pont azt csinalja amit iger a valtozo neve. sajnos ez nem az amit a feladat ker.
probald ki egy [1,2,3,1] teszt listaval.



# 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
mergedList= list()
i =0
j=0
while (i < len(list1)) and (j < len (list2) ):
if list1[i] <= list2[j]:
mergedList.append(list1[i])
i= i+1
elif list1[i] > list2[j]:
mergedList.append(list2[j])
j=j+1
while (i < len(list1)):
mergedList.append(list1[i])
i= i+1
while (j < len(list2)):
mergedList.append(list2[j])
j = j + 1
return mergedList
Copy link
Owner

Choose a reason for hiding this comment

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

szuper 👍



# Note: the solution above is kind of cute, but unforunately list.pop(0)
Expand Down
28 changes: 18 additions & 10 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 > 9:
return "Number of donuts: many"
else:
return "Number of donuts: " + str(count)


# B. both_ends
Expand All @@ -34,11 +36,15 @@ 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:
s = ''
return s
else:
return s[0] + s[1] + s[-2] + s[-1]

# C. fix_start


# C. fix_start
# Given a string s, return a string
# where all occurences of its first char have
# been changed to '*', except do not change
Expand All @@ -48,8 +54,9 @@ 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
listOfChars = list(s.replace(s[0], '*'))
listOfChars[0] = s[0]
return ''.join(listOfChars)


# D. MixUp
Expand All @@ -60,14 +67,15 @@ 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
aSwapped = b[:2] + a[2:]
bSwapped = a[:2] + b[2:]
return aSwapped + ' ' + bSwapped


# Provided simple test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
if got == expected:
if got== expected:
prefix = ' OK '
else:
prefix = ' X '
Expand Down
39 changes: 32 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:
if s[-3:] == "ing":
s = s + "ly"
else:
s = s + "ing"
return s


# E. not_bad
Expand All @@ -29,9 +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

index = s.find("not")
index2 = s.find("bad")
if index > 0 and index2 > 0 and index < index2:
s = s[:index] + "good" + s[index2 + 3:]
return s

# F. front_back
# Consider dividing a string into two halves.
Expand All @@ -41,8 +47,27 @@ 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
la = len(a)
lb = len(b)
afront = ""
bfront = ""
aback = ""
bback = ""
if divmod(la, 2) == 0:
afront = a[:la/2]
aback = a[(la/2) + 2:]
else:
afront = a[:(int)((la+1) / 2)]
aback = a[(int)((la-1) / 2):]

if divmod(lb, 2) == 0:
bfront = b[:lb/2]
bback = b[(lb/2) + 2:]
else:
bfront = b[:(int)((lb+1) / 2)]
bback = b[(int)((lb-1) / 2):]

return afront + bfront + aback + bback


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