0

I'm trying to generate 20 random numbers in my list. Next I want to see if there are any repeated numbers in it.

I generated the random numbers. Next I did an insertion sort in order to arrange the list in a growing order. To see if I had any repeated numbers, I iterated over the list and if the previous number was equal to the next number, then my list had repeated numbers in it.

import random

random_array=[]

def array():
  for i in range(0,20):
    random_element=random.randint(1,35)
    random_array.append(random_element)
  return random_array
print(array())

# function to order the list
def insertion_sort():
  for i in range(1,len(random_array)):
    while i>0 and random_array[i-1]>random_array[i]:
      random_array[i-1],random_array[i]=random_array[i],random_array[i-1]
      i=i-1
  return random_array
print(insertion_sort())

def verification():
  for i in random_array:
    insertion_sort()
    if i-1==i:
      return True
    else:
      return False
print(verification())

My program always returns false independent of the generated list.

Marie
  • 149
  • 9
Luismaia1994
  • 143
  • 1
  • 8

5 Answers5

1

Issues in your code:

  • You are iterating on the elements and subtracting them and comparing, instead you should iterate on the indexes and use them to compare consecutive elements

  • You are calling insertion_sort() within your for loop, I assume by mistake

  • You want to break the for loop on the first match you found, and in the end return a boolean which tells if the match has happened or not

So your code will look like

def verification():
    #Flag to keep track of matches
    flag = False
    #Iterate over list via indexes
    for i in range(len(random_array)):
        #If consecutive elements match, set flag to true and break loop
        if random_array[i-1]==random_array[i]:
            flag = True
            break
    #Return flag value
    return flag
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

You have two options here: If you don't care about the order, you can use set which is a data type that can only have unique elements. If you need to keep your original order of numbers, you need to use OrderedDict from collections

See this stack answer: https://stackoverflow.com/a/7961393/11323304

zerecees
  • 697
  • 4
  • 13
0

your loop in def verification(): is exiting after first iteration because if else: return False (it's exiting)

use simple flag:

import random

random_array=[]

def array():
  for i in range(0,20):
    random_element=random.randint(1,35)
    random_array.append(random_element)
  return random_array
print(array())

# function to order the list
def insertion_sort():
  for i in range(1,len(random_array)):
    while i>0 and random_array[i-1]>random_array[i]:
      random_array[i-1],random_array[i]=random_array[i],random_array[i-1]
      i=i-1
  return random_array
print(insertion_sort())

def verification():
  #for i in random_array:
  status = False # <----
  for i in range(len(random_array)-1): # <------
    insertion_sort()
    #if i-1==i:
    if random_array[i] == random_array[i+1]: # Iterate over list by indexes
      status = True
      break 
  return status
print(verification())
ncica
  • 7,015
  • 1
  • 15
  • 37
0

Your verification function is wrong. You are comparing the value of element i to the value of element i - 1, which is never going to be true. Try this:

insertion_sort()
for i in range(1,len(random_array - 1)):
    if random_arry[i] == random_array[i-1]:
        return True
return False  # If you get through every element without finding a match, return False

Marie
  • 149
  • 9
0

You can do it with much shorter code using itertools.Counter:

import random
from collections import Counter

# Generate a random array
random_array = [random.randint(1, 35) for _ in range(20)]


nums = [
    n  # A number
    for (n, count) in Counter(random_array).items()  # From countered items
    if count > 1  # If the count of this number is more than one
]
print(nums)
vurmux
  • 9,420
  • 3
  • 25
  • 45
  • the goal also is to understand functions. I'm not interest in shorter codes yet. I know there are much easier ways to do the problem.. – Luismaia1994 Jun 14 '19 at 22:44