Python Forum
how do i add new items to a dictionary ?
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i add new items to a dictionary ?
#1
"""
Create a function called fizz_buzz_advanced. The function should take 3
arguments:
   - a dictionary, which maps integer numbers to words, K:W (but could be empty)
   - a number describing the start of a range (INclusive), L
   - a number describing the end of the ranage (EXclusive), U

The function must return a new dictionary, where:
  1. The result keys (KR) would be the integer numbers within the range,
    confined between L and U.
  2. The corresponding values would be composed of the values of the
    concatenated M words, whose key divides the result key with no remainder
    (in other words, where KR modulo K = 0).
  3. The order of the concatenation is important, and it should follow the
    ascending order of the keys (K) in the input dict
  4. If none of the keys K divides the KR exactly, then resulting value should
    be '' (an emptystring).

Please check the given example.

In addition, the function should have the following string as its documentation:
  Advanced version of FizzBuzz solver

Example:
fizz_buzz({3:'Fizz', 5:'Buzz'}, 2, 16) ->

{
  1:'',
  2:'',
  3:'Fizz',  # 3 is divisble by 3 with a remainder of 0, so we return Fizz
  4:'',
  5:'Buzz',  # 5 is divisble by 5 with a remainder of 0, so we return Buzz
  6:'Fizz',  # 6 is divisble by 3 with a remainder of 0, so we return Fizz
  7:'',
  8:'',
  9:'Fizz',  # 9 is divisble by 3 with a remainder of 0, so we return Fizz
  10:'Buzz', # 10 is divisble by 5 with a remainder of 0, so we return Buzz
  11:'',
  12:'Fizz', # 12 is divisble by 3 with a remainder of 0, so we return Fizz
  13:'',
  14:'',
  15:'FizzBuzz', # 15 is divisible by both 3 and 5, so we return FizzBuzz
                 # Note that the order should always be "Fizz" first and
                 # "Buzz" second, due to "Fizz" having a lower value (3)
                 # in the input dictionary (compared to 5 for "Buzz")
}
"""

def fizz_buzz_advanced(db,ll,ul):
    for i in range(ll,ul):
        for k,v in db.items():
            p = i%k
            ndb = {}
            if (p == 0):
                n = v
                ndb.update({i:n})
            else:
                n = ''
                ndb.update({i:n})
                print ndb

db = {3:'Fizz', 5:'Buzz'}
ll = 2
ul = 7
fizz_buzz_advanced(db,ll,ul)
my question:
how do i append new items to a dictionary
Reply
#2
Take a look at http://python-forum.io/Thread-Basic-Dictionaries
Reply
#3
ok... the appending part seems to be working now...
I have modified the code a bit....


def fizz_buzz_advanced(db,ll,ul):
    ndb = {}
    for i in range(ll,ul):
        for k,v in db.items():
            p = i%k
            if (p == 0):
                ndb[i] = v
            else:
                n = ''
                ndb[i] = n
    print ndb

db = {3:'Fizz', 5:'Buzz'}
ll = 2
ul = 7
fizz_buzz_advanced(db,ll,ul)
but am not getting the desired output now...
Reply
#4
ndb[i] = v is where your problem lies.  Your output always has Buzz in the right places, but you never see Fizz.  That's because you always compare Fizz first, set it where it needs to be, and then compare for Buzz and overwrite whatever you did for Fizz.

One possible solution would be to do ndb[i] = "" before you iterate over the db, and then when a match is found, you simply add it to whatever was already there with += .
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020