-3

I have a dict like:

a = {'1': [100,95,10,80,82,78,8,7,9,2], 
     '2': [98,99,11,82,107,78,8,69,2], 
     '3': [10,98,10,80,20,45,46,50,4,2]} 

If the list of trend drops below 80% and continues below 80% code will print "trend is under 80%". The last value of keys will be excluded.

For example In '1': [100,95,10,80,82,78,8,7,9,2] the last value is 2 so it will not be calculated.

Another example: In '1': [100,95,10,80,82,78,8,7,9] 100,95 drops over 80% to 10 and rises to 80 and continues around 80 and then drops to around 8. If this pattern is found in the code, it will write "value decreased under 80%".

To summarize, the value will drop over 80% and stability will continue under 80% till the end.

I can't use any library or define any function, only with if, else, for, while and something like that.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user289017
  • 15
  • 6

1 Answers1

-1
a = {'1': [100,95,10,80,82,78,8,7,9,2], 
     '2': [98,99,11,82,107,78,8,69,2], 
     '3': [10,98,10,80,20,45,46,50,4,2]} 

for key, values in a.items():
    below_80 = False
    for value in values[:-1]:
        if value < 80:
            below_80 = True
        elif below_80 and value >= 80:
            print(f"Trend for {key} is under 80%")
            break
    if below_80:
        print(f"Value for {key} decreased under 80%")

This code loops through each key-value pair in the dictionary a. For each value list, it checks if the values are below 80. If a value is below 80, it sets the flag below_80 to True. If below_80 is True and a value is equal or greater than 80, it prints "Trend for [key] is under 80%". After the inner loop, if below_80 is still True, it prints "Value for [key] decreased under 80%".

Chirag Maliwal
  • 442
  • 11
  • 25