Home > Net >  Simplify if conditions in python
Simplify if conditions in python

Time:12-05

Is there a neat way to simplify this if statement? I need n to be >= 2 and <= 100 and value to increase by one for every seventh step (except the first one which should be between 2 and 7 and last one which should be between 98 and 100).

if n >= 2 and n <= 7:
    value = 1
elif n > 7 and n <= 14:
    value = 2
elif n > 14 and n <= 21:
    value = 3
elif n > 21 and n <= 28:
    value = 4
elif n > 28 and n <= 35:
    value = 5
elif n > 35 and n <= 42:
    value = 6
elif n > 42 and n <= 49:
    value = 7
elif n > 49 and n <= 56:
    value = 8
elif n > 56 and n <= 63:
    value = 9
elif n > 63 and n <= 70:
    value = 10
elif n > 70 and n <= 77:
    value = 11
elif n > 77 and n <= 84:
    value = 12
elif n > 84 and n <= 91:
    value = 13
elif n > 91 and n <= 98:
    value = 14
elif n > 98 and n <= 100:
    value = 15

EDIT: Furthermore I'm having trouble finding the smallest number possible from the following question:


Matchsticks are ideal tools to represent numbers. A common way to represent the ten decimal digits with matchsticks is the following:

enter image description here

This is identical to how numbers are displayed on an ordinary alarm clock. With a given number of matchsticks you can generate a wide range of numbers. We are wondering what the smallest and largest numbers are that can be created by using all your matchsticks.

Input:

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with an integer n (2 <= n <= 100): the number of matchsticks you have.

Output:

Per testcase:

One line with the smallest and largest numbers you can create, separated by a single space. Both numbers should be positive and contain no leading zeroes.


I've tried multiple different ways to try to solve this problem, I'm currently trying to:

  1. find the minimal number of digits (value) for the smallest number:

#Swifty

def values(n):
    if 2 <= n <= 100:
        value = (n   6) // 7
    minimum(n, value)
  1. now i want to send value to a function minimum() which should generate all the different combinations of numbers that are the length of value. I want to store all these numbers in a list and then take min() to get the smallest one. I'm not getting this part to work, and would appreciate some inspiration.

Something to remember is that the number can't start with a 0.

CodePudding user response:

if 2 <= n <= 100:
    value = (n 6)//7

CodePudding user response:

Here is one way to simplify the if statement you provided in Python:

value = (n // 7)   1 if 2 <= n <= 100 else None

This code uses integer division (the "//" operator) to divide the value of "n" by 7 and add 1 to the result. If the value of "n" is less than or equal to 100, this will give the same result as the original if statement. Otherwise, it will set "value" to None.

Here is an example of how this code would work:

# n = 14
value = (14 // 7)   1  # This evaluates to 2

# n = 100
value = (100 // 7)   1  # This evaluates to 15

# n = 101
value = (101 // 7)   1  # This evaluates to None

I hope this helps! Let me know if you have any other questions.

  • Related