ynonp / python-examples-verint-2016-07

Python examples and exercises
MIT License
2 stars 35 forks source link

uploading exercises #67

Open HananRosenthal opened 8 years ago

HananRosenthal commented 8 years ago

In Data Strustures chapter:

  1. I have a problem uploading the hosts exercise
  2. also there are 01 - 05.py to submit - but where are these exercises?
szabgab commented 8 years ago

We are on it. In the meantime, please submit only the files in the text.

HananRosenthal commented 8 years ago

what are the correct names of the files in the text?

HananRosenthal commented 8 years ago

Hi, I am using the following input: add dad help more rome groom romog

My code follows:

import argparse
from  collections import Counter

parser = argparse.ArgumentParser(description='look for anagrams in the input file')
parser.add_argument('-p','--path', help='', required = True)
kuku = parser.parse_args()

li=[]
freq={}
li1=[]

#Populate a list with all words in the input file (each word contains the \n at its end)
with open(kuku.path, 'r') as f:
    for line in f:
    li.append(line)

#print(li)
for word in li:
    for letter in word:
        count = freq.setdefault(letter,0)
        freq[letter] = count + 1
    li1.append(freq)
    freq.clear()

I would expect the list (li1) to contain all words letter counting, however it contains the same item all over. Why is that? 10X

ynonp commented 8 years ago

Hi Hanan,

This is a very interesting issue! Here's the short explanation with some code example to clarify:

  1. Python's dictionary is a mutable data structure (which means it changes with time)
  2. Assignment operator does not create a new dictionary, but rather a new reference to the same dictionary. Take the following snippet:
a = { 'a': 10, 'b': 20 }
b = a
a.clear()

But now b also points to an empty dict.

  1. Your code appends the same dictionary (freq) over and over, each time empties it right after insertion. That explains why your list has just one dictionary appearing multiple times.
  2. The function dict(...) creates a new dictionary, so the following modification works as expected:
import argparse
from  collections import Counter

parser = argparse.ArgumentParser(description='look for anagrams in the input file')
parser.add_argument('-p','--path', help='', required = True)
kuku = parser.parse_args()

li=[]
freq={}
li1=[]

#Populate a list with all words in the input file (each word contains the \n at its end)
with open(kuku.path, 'r') as f:
    for line in f:
    li.append(line)

#print(li)
for word in li:
    for letter in word:
        count = freq.setdefault(letter,0)
        freq[letter] = count + 1
    li1.append(dict(freq))
    freq.clear()
HananRosenthal commented 8 years ago

when trying to use 'reduce', i get "NameError: name 'reduce' is not defined" ?

szabgab commented 8 years ago

reduce has been removed in Python 3 https://docs.python.org/3.0/whatsnew/3.0.html#builtins You can revive it using from functools import reduce or you need to find another way to solve the problem.