Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 25 additions & 23 deletions basic/list1.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/

# Basic list exercises
# Fill in the code for the functions below. main() is already set up
# to call the functions with a few different inputs,
# printing 'OK' when each function is correct.
# The starter code for each function includes a 'return'
# which is just a placeholder for your code.
# It's ok if you do not complete all the functions, and there
# are some additional functions to try in list2.py.

# A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# 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
n = 0
for word in words:
if len(word) >= 2 and word[0] == word[len(word) - 1]:
n += 1
return n


# B. front_x
Expand All @@ -33,8 +19,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
listx = []
listnorm = []
for word in words:
if word[0] == 'x':
listx.append(word)
else:
listnorm.append(word)

return sorted(listx) + sorted(listnorm)


# C. sort_last
Expand All @@ -44,8 +37,17 @@ 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
endings = []
res = []
for e in tuples:
endings.append(e[-1])
endings.sort()
for ending in endings:
for tup in tuples:
if tup[-1] == ending:
res.append(tup)
return res

Copy link
Owner

Choose a reason for hiding this comment

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

wow :) Klassz megoldas, bar egy kicsit lassu. https://wiki.python.org/moin/HowTo/Sorting
key functions alatt talasz elegansabb megoldast.



# Simple provided test() function used in main() to print
Expand Down
10 changes: 4 additions & 6 deletions basic/list2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python -tt
# !/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -13,17 +13,15 @@
# 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
return list(set(nums))
Copy link
Owner

Choose a reason for hiding this comment

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

haat... nem egeszen. mit gondolsz jo eredmenyt kapnal mondjuk [1,2,3,3,1] listara?

Copy link
Author

Choose a reason for hiding this comment

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

Ja igen elnéztem, azt hittem csak simán halmazként kell visszaadni. Javítom majd:)



# 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
return sorted(list1 + list2)
Copy link
Owner

Choose a reason for hiding this comment

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

nem rossz probalkozas, de a python sorted TimSort algoritmussal dolgozik nlogn idoben. A feladat leirasaban a megoldas; a ket lista eleve rendezett. ;)



# Note: the solution above is kind of cute, but unforunately list.pop(0)
Expand Down Expand Up @@ -62,4 +60,4 @@ def main():


if __name__ == '__main__':
main()
main()
25 changes: 15 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 < 10:
return "Number of donuts: " + str(count)
else:
return "Number of donuts: many"


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


# C. fix_start
# Given a string s, return a string
# where all occurences of its first char have
# where all occurrences of its first char have
# been changed to '*', except do not change
# the first char itself.
# e.g. 'babble' yields 'ba**le'
# Assume that the string is length 1 or more.
# 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
first = s[0]
rest = s[1:]
res = rest.replace(first, '*')
return first + res


# D. MixUp
Expand All @@ -60,8 +66,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 Expand Up @@ -108,4 +113,4 @@ def main():

# Standard boilerplate to call the main() function.
if __name__ == '__main__':
main()
main()
28 changes: 20 additions & 8 deletions basic/string2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
# 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) > 2:
if s[-3:] != 'ing':
return s + 'ing'
else:
return s + 'ly'
else:
return s


# E. not_bad
Expand All @@ -29,9 +34,11 @@ def verbing(s):
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return

not_ind = s.find('not')
bad_ind = s.find('bad')
if not_ind != -1 and bad_ind != -1 and not_ind < bad_ind:
s = s[:not_ind] + 'good' + s[bad_ind + 3:]
return s

# F. front_back
# Consider dividing a string into two halves.
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
amid = len(a) / 2
bmid = len(b) / 2
if len(a) % 2 == 1:
amid += 1
if len(b) % 2 == 1:
bmid += 1
return a[:int(amid)] + b[:int(bmid)] + a[int(amid):] + b[int(bmid):]


# Simple provided test() function used in main() to print
Expand Down Expand Up @@ -76,4 +88,4 @@ def main():


if __name__ == '__main__':
main()
main()