Skip to content

Elso hazi #21

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 5 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
29 changes: 23 additions & 6 deletions basic/list1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
# 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
counter = 0;
for word in words:
wordlen = len(word);
if wordlen > 1:
if(word[wordlen-1] == word[0]):
counter+=1;
return counter;


# B. front_x
Expand All @@ -33,8 +38,16 @@ 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
xlist = list();
nlist = list();
for word in words:
if word[0]=='x':
xlist.append(word);
else:
nlist.append(word);
xlist = sorted(xlist);
nlist = sorted(nlist);
return xlist + nlist;


# C. sort_last
Expand All @@ -43,9 +56,13 @@ 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 tuple_last(tuplee):
for i,var in enumerate(tuplee):
if i == len(tuplee) - 1:
return var;
Copy link
Owner

Choose a reason for hiding this comment

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

ezzel azert messzire mentel hogy megkapd a tuplee[-1] elemet :)


def sort_last(tuples):
# +++your code here+++
return
return sorted(tuples, key = tuple_last);


# Simple provided test() function used in main() to print
Expand Down
27 changes: 23 additions & 4 deletions basic/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,36 @@
# 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
for num in nums:
if nums.count(num) > 1:
nums.remove(num);
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
lista = list()
count1 = 0
count2 = 0
while len(list1)> count1 and len(list2) > count2:
if list1[count1] < list2[count2]:
lista.append(list1[count1])
count1 += 1
else:
lista.append(list2[count2])
count2 += 1
if len(list1)> count1:
while(len(list1)> count1):
lista.append(list1[count1])
count1 += 1
if len(list2) > count2:
while(len(list2)> count2):
lista.append(list2[count2])
count2 += 1
return lista


# 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
fileobj = open(filename,"r")
filestr = fileobj.read().split()
filestr.insert(0,"")
words = {}
for i in range(len(filestr)-1):
if filestr[i] not in words:
words[filestr[i]] = [filestr[i+1]]
else:
lista = words[filestr[i]]
lista.append(filestr[i+1])
words[filestr[i]] = lista
return words


def print_mimic(mimic_dict, word):
"""Given mimic dict and start word, prints 200 random words."""
# +++your code here+++
last_word= word
for i in range(200):
if last_word in mimic_dict:
lista = mimic_dict[last_word]
print(last_word)
last_word = lista[random.randint(0,len(lista)-1)]
else:
lista = mimic_dict[word]
print(last_word)
last_word = lista[random.randint(0,len(lista)-1)]
return


Expand Down
40 changes: 32 additions & 8 deletions basic/string1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
# +++your code here+++
return
strr = 'Number of donuts: '
if count > 9:
strr += 'many'
return strr
else:
strr += str(count)
return strr


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


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


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


# Provided simple test() function used in main() to print
Expand Down
50 changes: 43 additions & 7 deletions basic/string2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
# 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.rfind('ing') == len(s)-3:
s += 'ly'
return s
else:
s+= 'ing'
return s
else:
return s


# E. not_bad
Expand All @@ -29,8 +36,15 @@ def verbing(s):
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return
appn = s.find('not')
appb = s.find('bad')
if appn < appb:
string=''
string += s[0:appn]
string += 'good'
string += s[appb+3:len(s)]
return string
return s


# F. front_back
Expand All @@ -41,9 +55,31 @@ 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

afront=''
aback=''
bfront=''
bback=''
if len(a) % 2 == 0:
afront=a[0:int(len(a)/2)]
aback=a[int(len(a)/2):len(a)]
else:
szam = int(len(a)/2)
afront=a[0:szam+1]
aback=a[szam+1:len(a)]

if len(b) % 2 == 0:
bfront=b[0:int(len(b)/2)]
bback=b[int(len(b)/2):len(b)]
else:
szam = int(len(b)/2)
bfront=b[0:szam+1]
bback=b[szam+1:len(b)]
final=''
final+=afront
final+=bfront
final+=aback
final+=bback
return final

# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
Expand Down
35 changes: 33 additions & 2 deletions basic/wordcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,39 @@
# and builds and returns a word/count dict for it.
# Then print_words() and print_top() can just call the utility function.

###

def print_words(filename):
fileobj = open(filename,"r")
words = {}
for line in fileobj:
for word in line.split():
if word not in words:
words[word] = 1
else:
words[word] += 1
print(str(words) + "\n")




def print_top(filename):
fileobj = open(filename,"r")
words = {}
for line in fileobj:
for word in line.split():
if word not in words:
words[word] = 1
else:
words[word] += 1
from operator import itemgetter
words = sorted(words.items(), key=itemgetter(1),reverse=True)
i=0
for k in words:
if(i == 20):
break
print(k)
i += 1


# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
def main():
Expand Down