Home > Software design >  Cycle where numeber are entered and where none is repeate
Cycle where numeber are entered and where none is repeate

Time:05-13

I have to make a program where numbers are entered. where none of the elements is repeated. The loop ends when 999 is entered, and YOU MUST NOT USE THE LIST. When you ENTER 0 it separates elements by group, indicates the number of elements and indicates the smallest element.

How do I make sure that no element is repeated without using a list?

group=0
element=0
n=int(input("Number= "))
minor=9999999

while n != 999:
          
    if n == 0:
        group =1
        print ("group= ", group , " ," , "number of element", element, "," , "Minor element= ", minor)
        element= 0
        minor=999999
    else:
        element =1
        for i in range (element):
                 if n < minor:
                     minor = n
    n=int(input("Number= "))
    
 
group =1
print ("group= ", group , " ," , "Number of element", element, "," , "minor element= ", minor)
print("------------------")
print("Numbers of groups= ", group)
print("end...........")

CodePudding user response:

You cannot use list, but perhaps you can use a dictionary. This seem like a perfect use case for key/value pair. The key would be the element and the value the number of times this element as been seen.

If you can use it, heres a great articles on how to use dictionary in python.

Hopes this helps !

https://www.programiz.com/python-programming/dictionary

CodePudding user response:

if you really don't want to use a list, the best thing i can think of is writing to a file and reading that file after every number input i THINK this might work

group=0
element=0
n=int(input("Number= "))
f = open('numbers.txt', 'a')
f.write(f'{n}\n')
f.close()
minor=9999999

while n != 999:
          
    if n == 0:
        group =1
        print ("group= ", group , " ," , "number of element", element, "," , "Minor element= ", minor)
        element= 0
        minor=999999
    else:
        element =1
        for i in range (element):
                 if n < minor:
                     minor = n

    while True:
      print(open('numbers.txt', 'r').read().splitlines())
      n=int(input("Number= "))
      if n not in [int(x.strip('\n')) for x in open('numbers.txt', 'r').readlines()]:
        f = open('numbers.txt', 'a')
        f.write(f'{n}\n')
        f.close()
        break
    
 
group =1
print ("group= ", group , " ," , "Number of element", element, "," , "minor element= ", minor)
print("------------------")
print("Numbers of groups= ", group)
print("end...........")

I’m aware I still use a list, but I’m not defining it as a variable so I’m sure it doesn’t count

  • Related