Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Brute-force algorithms", exercise 4 #19

Open
essepuntato opened this issue Oct 30, 2020 · 17 comments
Open

Lecture "Brute-force algorithms", exercise 4 #19

essepuntato opened this issue Oct 30, 2020 · 17 comments
Labels

Comments

@essepuntato
Copy link
Contributor

Write in Python the function def my_range(stop_number) which behave like the built-in function range() introduced in Section "Insertion sort" and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function range() in the implementation.

@edoardodalborgo
Copy link

def test_my_range(stop_number):
    counts = 0
    for a in my_range(stop_number):
        counts += 1
    if counts == stop_number:
        return True
    else: return False

def my_range(stop_number):
    my_range_list = []
    i = 0
    while i < stop_number:
        my_range_list.append(i)
        i += 1
    return my_range_list

x = int(input("Enter the range that you would obtain: "))
print(test_my_range(x))

@diegochillo
Copy link

def my_range(stop_number):
   my_range=list()
   n=0
   while (n<stop_number):
	my_range.insert(n,n)
	n=n+1
	
   return my_range


def test_my_range(stop_number, expected):
   result=my_range(stop_number)
   return result==expected

print(test_my_range(5, [0,1,2,3,4]))

@gabrielefiorenza
Copy link

def test_my_range(stop_number,expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False

def my_range(stop_number):
    result = []
    number = 0
    result.append(number)
    while number != (stop_number -1):
        number += 1
        result.append(number)
    return result


print (test_my_range(4,[0,1,2,3]))
print (test_my_range(3,[0,1]))

@dbrembilla
Copy link

def test_my_range(stop_number, expected):
    result=my_range(stop_number)
    return result ==expected

def my_range(stop_number):
    x = 0
    my_range_list = list()
    while x < stop_number:
        my_range_list.append(x)
        x += 1
    return my_range_list

print(test_my_range(4, [0 , 1, 2, 3]))

@ChiaraCati
Copy link

# Test
def test_my_range(stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False

# Function    
def my_range(stop_number):
    my_list = []
    c=0
    while c < stop_number:
        my_list.append(c)
        c+=1
    return my_list
    
#print(my_range(6))

print(test_my_range(6, ([0, 1, 2, 3, 4, 5]))) # Output : True
print(test_my_range(4, ([1, 2, 3, 4])))  # Output : False
print(test_my_range(0, ([0]))) # Output : False
print(test_my_range(0, ([]))) # Output : True

me and Giorgia Sampò did it together

👭

@fcagnola
Copy link

# Write in Python the function def my_range(stop_number) which behaves like the
# built-in function range() introduced in Section "Insertion sort" and returns
# a proper list, and accompany the function with the related test case. It is
# not possible to use the built-in function range() in the implementation.

def test(stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False

def my_range(stop_number):
    result_list = [] # creates the final result, as a list object
    number = 0 # sets the starting point at 0
    while number != stop_number: # loops through integers between 0 and stop_number
        result_list.append(number) # adds each loop value to the final list
        number += 1
    return result_list #returns the result

print(test(5, [0, 1, 2, 3, 4])) # returns True
print(test(19, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])) # returns True

@AleRosae
Copy link

AleRosae commented Nov 3, 2020

def test_my_range (stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False

def my_range(stop_number):
    n = 0
    my_list = list ()
    while n != stop_number:
        my_list.append(n)
        n=n + 1


    return my_list

print(test_my_range((4), [0, 1, 2, 3]))
print(test_my_range((10), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
print(test_my_range((0), []))

@AlessandraFa
Copy link

def test_my_range(stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False


def my_range(stop_number):
    a = 0
    range_list = list()
    while a < stop_number:
        range_list.append(a)
        a += 1
    return range_list


print(test_my_range((8), [0, 1, 2, 3, 4, 5, 6, 7]))
print(test_my_range((1), [0]))

@yunglong28
Copy link

#This is the function that simulates range()

def my_range (stop_number):
    range = []
    n = 0
    while n < stop_number:
        range.append(n)
        n=n+1
    return range
print(my_range(8))
#This is the test function
def test_myrange (stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False
print(test_myrange(8, [0, 1, 2, 3, 4, 5, 6, 7]))

@SusannaPinotti
Copy link

def test_my_range(stop_number, expected):
    res = my_range(stop_number)
    if expected == res:
        return True
    else:
        return False

def my_range(stop_number):
    output_list = list()
    n = 0
    while n < stop_number:
        output_list.append (n)
        n += 1
    return output_list

print(test_my_range((6), [0,1,2,3,4,5])) #return True
print(test_my_range((0), [])) #returns True
print(test_my_range((0), [0])) #return False

@IlaRoss
Copy link

IlaRoss commented Nov 5, 2020

#test
def test_my_range(stop_number, range_list):
    result = my_range(stop_number)
    if result == range_list:
        return True
    else:
        return False

#algorithm
def my_range(stop_number):
    range_list = list()
    prov_list = list()
    prov_number = (stop_number-1)

    if stop_number == 0:
        return range_list

    elif stop_number < 0:
        return 'Error'

    else:

        while prov_number >= 0:
            prov_list.append(prov_number)
            prov_number -= 1

        for item in prov_list:
            range_list.insert(0, item)

        return range_list



#test runs
print(test_my_range(5, [0, 1, 2, 3, 4]))
print(test_my_range(0, []))
print(test_my_range(-3, 'Error'))

#algorithm execution
print(my_range(12))
print(my_range(0))
print(my_range(-15))

@LuisAmmi
Copy link

LuisAmmi commented Nov 5, 2020

# test
def test_my_range(stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    else:
        return False


# code
def my_range(stop_number):
    output_list = list()
    n = 0                  # initialize n
    while n != stop_number:
        output_list.append(n)
        n += 1
    else:
        return output_list


print(test_my_range(3, [0, 1, 2]))
print(test_my_range(2, [0, 1]))
print(test_my_range(0, []))```

@vanessabonanno
Copy link

def test_my_range(stop_number, expected):
    result = my_range(stop_number)
    if expected == result:
        return True
    else:
        return False


def my_range(stop_number):
    my_list = []
    x = 0
    while x < stop_number:
        my_list.append(x)
        x += 1
    return my_list


print(test_my_range(4, [0, 1, 2, 3]))# output: True
print(test_my_range(2, [1, 2]))      # output: False (starting number will be 0,
                                     # not 1 because x=0; 2 is not included in list since
                                     # there is '<' and not 'less or equal to')
print(my_range(4))                   # [0, 1, 2, 3]


@AlessandroBertozzi
Copy link

def test_my_range(stop_number, expected):
    result = my_range(stop_number)
    if result == expected:
        return True
    return False


def my_range(stop_number):
    counter = 0
    list_number = []
    while counter < stop_number:
        list_number.append(counter)
        counter += 1
    return list_number


print(test_my_range(3, [0, 1, 2]))
print(test_my_range(7, [0, 1, 2, 3, 4, 5, 6]))
print(test_my_range(0, []))

@SarahTew
Copy link

SarahTew commented Nov 7, 2020


def my_range(stop_number):
    if stop_number>=0:
        n=0
        range_result=list()
        while n<stop_number:
            range_result.append(n)
            n=n+1
        return(range_result)
    else:
        return("Negative Number!")
        
#print ranges    
print(my_range(5))
print(my_range(3))
print(my_range(-5))
print(my_range(0))
print(my_range(3.3))

#test my_range function
def test_my_range(stop_number, expected):
    result=my_range(stop_number)
    if result==expected:
        return True
    else:
        return False
print(test_my_range(3, [0, 1, 2]))
print(test_my_range(0, []))
print(test_my_range(-9, "Negative Number!"))

@essepuntato
Copy link
Contributor Author

Hi all,

A general comment: can you try to run the range function in Python with the following input, to see what actually happens? Does your function work as the original one?

range(-4)

@SofiBar
Copy link

SofiBar commented Nov 12, 2020

exercise 4:

`def test_my_range(stop_number, expected):
    if expected == my_range(stop_number):
        return True
    else:
        return False

def my_range(stop_number):
    count = 0
    output_list = []

    while count < stop_number:
        output_list.append(count)
        count += 1
    return output_list


print(test_my_range(5, [0, 1, 2, 3, 4]))
print(test_my_range(7, [0, 1, 2, 3, 4, 5, 6]))
print(test_my_range(4, [0, 1, 2, 3]))

Range issue:
if I write a negative number my function returns an empty list because -4 is less than 0 so the condition is never true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests