Skip to content

Elso hazi #20

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 2 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
25 changes: 19 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
c = 0;
for x in words:
if len(x) >= 2 and x[0] == x[len(x) - 1]:
c += 1
return c;


# 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
xWords = []
wordss = []
for x in words:
if x[0] == 'x':
xWords.append(x)
else:
wordss.append(x)

return sorted(xWords) + sorted(wordss)


# C. sort_last
Expand All @@ -43,9 +53,12 @@ def front_x(words):
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key= function to extract the last element form each tuple.
def callMe(e):
return e[len(e) - 1]

def sort_last(tuples):
# +++your code here+++
return
tuples.sort(key=callMe)
return tuples


# Simple provided test() function used in main() to print
Expand Down
14 changes: 10 additions & 4 deletions basic/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
# 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
temp = []
if len(nums) != 0:
temp.append(nums[0])

for i in range(1, len(nums)):
if nums[i - 1] != nums[i]:
temp.append(nums[i])

return temp


# 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.

doesn't seems to be working in "linear" time



# Note: the solution above is kind of cute, but unforunately list.pop(0)
Expand Down
27 changes: 22 additions & 5 deletions basic/mimic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,31 @@


def mimic_dict(filename):
"""Returns mimic dict mapping each word to list of words which follow it."""
# +++your code here+++
return
f = open(filename)
words = f.read().split()
f.close()
dict = {}

first = ''
for i in range(0, len(words)):
if(first not in dict.keys()):
dict[first] = []

dict[first].append(words[i])

first = words[i]

return dict


def print_mimic(mimic_dict, word):
"""Given mimic dict and start word, prints 200 random words."""
# +++your code here+++


for i in range(0, 200):
print(word + ' ')
if(word not in mimic_dict.keys()):
word = ''
word = random.choice(mimic_dict[word])
return


Expand Down
29 changes: 21 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
res = count
if count > 9:
res = 'many'
return 'Number of donuts: {0}'.format(res)


# B. both_ends
Expand All @@ -34,8 +36,10 @@ 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
s_len = len(s)
if s_len < 2:
return ''
return s[0] + s[1] + s[s_len - 2] + s[s_len - 1]


# C. fix_start
Expand All @@ -48,8 +52,11 @@ 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
save = s[0]
s2 = s.replace(s[0], '*')
s2 = list(s2)
s2[0] = save
return ''.join(s2)


# D. MixUp
Expand All @@ -60,8 +67,14 @@ 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 = list(a)
b = list(b)
temp = a[0:2]
a[0] = b[0]
a[1] = b[1]
b[0] = temp[0]
b[1] = temp[1]
return ''.join(a) + ' ' + ''.join(b)


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


# E. not_bad
Expand All @@ -29,8 +34,14 @@ def verbing(s):
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return
snot = s.find('not')
sbad = s.find('bad')
if ('bad' in s) and ('not' in s):
if snot < sbad:
firstPart = s[0 : snot]
lastPart = s[sbad + 3 : len(s)]
s = firstPart + 'good' + lastPart
return s


# F. front_back
Expand All @@ -41,8 +52,15 @@ 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_i = int(len(a) / 2)
b_i = int(len(b) / 2)

if(len(a) % 2 != 0):
a_i += 1
if(len(b) % 2 != 0):
b_i += 1

return a[:a_i] + b[:b_i] + a[a_i:] + b[b_i:]


# Simple provided test() function used in main() to print
Expand Down
32 changes: 31 additions & 1 deletion basic/wordcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
print_words() and print_top().

"""

import sys


Expand All @@ -46,6 +45,37 @@
# and builds and returns a word/count dict for it.
# Then print_words() and print_top() can just call the utility function.

from collections import Counter


def print_words(filename):
f = open(filename)
words = f.read().split()
f.close()
c = Counter()

for x in words:
c[x.lower()] += 1

for x in sorted(c.items()):
print(x[0] + ' ' + str(x[1]))

return

def print_top(filename):
f = open(filename)
words = f.read().split()
f.close()
c = Counter()

for x in words:
c[x.lower()] += 1

for x in sorted(c.most_common(20)):
print(x[0] + ' ' + str(x[1]))


return
###

# This basic command line argument parsing code is provided and
Expand Down