Home > Mobile >  Create a function that multiplies the keys and values of a dictionary while under the constraint of
Create a function that multiplies the keys and values of a dictionary while under the constraint of

Time:04-06

To preface, I'm not very experienced with python. I'm currently trying to learn with what I know and the tools at my disposal.

Summary

I want to make a function that takes in a dictionary then afterwards multiplies the keys and values of said-dictionary together. However, there are external conditions that I want that are making this problem difficult.

These conditions are:

  • if one of the values are negative, instead of multiplying the value by it's respective key, it multiples x amount of asterisks where x is the amount of characters in the corresponding key by abs(value).

  • the function can only use basic for/while loops and if statements. I am somewhat familiar with list comprehension from viewing similar questions online but I'm not great at it. Instead, I'd like to practice using more simpler methods.

The function itself should look something like dict_multiply(dict): and a test trial should look like dict_multiply({'Terra': 6, 'Cloud': -7, 'Squall': 8}) which returns 'TerraTerraTerraTerraTerraTerra***********************************SquallSquallSquallSquallSquallSquallSquallSquall'. Terra is repeated 6 times, the letters in ‘Cloud’ are converted into asterisks and multiplied by abs(-7), and Squall is repeated 8 times.

What I'm Trying

What I have tried doing is breaking down the problem into steps. I haven't put any of my code into a function yet for the convenience of not having to rerun it, but I've been experimenting with variables.

for instance, I wanted to try creating a condition for where negative values in the dictionary would be read as asterisks.

dictionary = {'Terra': 6, 'Cloud': -7, 'Squall': 8}

keylist = list(dictionary.keys())
valuelist = list(dictionary.values())

for values in valuelist:
    if values < 0:
        valuelist[valuelist.index(values)] = abs(values) * ('*')
valuelist

which returns [6, '*******', 8]. This is only a single component that I want from the function, but I think I'm getting somewhere. keylist is a list containing ['Terra', 'Cloud', 'Squall'] which I want to multiply by the valuelist I returned. I know I can multiply strings such as 'Terra' by int values such as 6, but I know multiplying two strings together such as '*******' and 'Cloud' is impossible. Instead, I would just want the asterisks returned but I'm unsure on how to create this condition.

The issue of actually multiplying the lists of ints by the list of strings is something I'm concerned about too. I've seen explanations for this step online but none of the explanations are at my level of coding (basic for/while loops).

disregarding this and assuming that multiplication somehow succeeds, I think it I should be left with a list of strings separated by commas which I know I can merge into a single string using ''.join(c) (assuming that c is the result of the step I'm having trouble with).

I'm sorry if my thought process may be confusing. I'm still in the process of learning python. Going forward, I want to be able to break down problems like these into steps and determine which steps I should think about first. Any feedback is appreciated.

CodePudding user response:

You can try running the following code:

def dict_multiply(d: dict):
  s = ""
  for key, val in d.items():
    if val > 0:
      s  = key * val
    else:
      s  = "*" * abs(val) * len(key)
  return s

print(dict_multiply({'Terra': 6, 'Cloud': -7, 'Squall': 8}))

Here, we loop through the keys and values of the input dict. If the value is greater than 0 (so positive), we add the key a value number of times. If it isn’t, we add the asterisk the absolute value of value times.

  • Related