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


Messages In This Thread
how do i add new items to a dictionary ? - by roadrage - Dec-08-2016, 05:52 PM
RE: datastructures... - by micseydel - Dec-08-2016, 06:41 PM
RE: datastructures... - by roadrage - Dec-08-2016, 07:13 PM
RE: datastructures... - by nilamo - Dec-08-2016, 07:30 PM

Forum Jump:

User Panel Messages

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