Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Cea mai lunga secventa de patrate perfecte si care au numarul de cifre in ordine descrescatoare
- import math
- '''
- Citire
- '''
- def read_array():
- """
- Reads an array of values and returns it.
- :return: array, the resulting array.
- """
- array = []
- item_count = str(input("How many values? "))
- if item_count.isnumeric() and int(item_count) > 0:
- for _ in range(int(item_count)):
- value = input("Value " + str(_) + ": ")
- if not value.isnumeric():
- array = []
- break
- else:
- array.append(int(value))
- print("")
- return array
- '''
- Cerinta 1
- '''
- def is_square_number(number):
- """
- Checks if the given number is a square number
- :param number: int, given number.
- :return: bool, False if given number is not a square number, True if otherwise.
- """
- if type(number) is not int:
- return False
- if number < 0:
- return False
- if math.sqrt(number) % 1.0 == 0:
- return True
- else:
- return False
- def test_is_square_number():
- assert is_square_number(0) is True
- assert is_square_number(-1) is False
- assert is_square_number(2) is False
- assert is_square_number(4) is True
- assert is_square_number(1) is True
- def first_assignment(array):
- """
- Returns the longest sequence of square numbers in given array.
- :param array: array, given array of numbers.
- :return: array, longest sequence of square numbers.
- """
- max_start_index = 0
- max_length = 0
- curr_start_index = 0
- curr_length = 0
- for _ in range(len(array)):
- if is_square_number(array[_]):
- if curr_length == 0:
- curr_start_index = _
- curr_length += 1
- if curr_length > max_length:
- max_length = curr_length
- max_start_index = curr_start_index
- else:
- curr_length = 0
- if max_length > 0:
- index_array = []
- for _ in range(max_start_index, max_start_index+max_length):
- index_array.append(_)
- return index_array
- def test_first_assignment():
- assert first_assignment([0, 1, "test", 4, 9, 16, 3.14, 64]) == [3, 4, 5]
- assert first_assignment(["test"]) is None
- assert first_assignment([-1, -524, "fine", 4, 4, 4, "test"]) == [3, 4, 5]
- '''
- Cerinta 2
- '''
- def number_of_digits(number):
- """
- Returns the number of digits of the given number.
- :param number: int, given number.
- :return: int, number of digits of given number.
- """
- if type(number) is float and number % 1.0 == 0.0:
- return len(str(abs(int(number))))
- if type(number) is not int:
- return 0
- return len(str(abs(number)))
- def test_number_of_digits():
- assert number_of_digits(123) == 3
- assert number_of_digits(1) == 1
- assert number_of_digits("uranus") == 0
- assert number_of_digits(-4) == 1
- def second_assignment(array):
- """
- Returns the longest sequence of numbers that have a decreasing number of digits.
- :param array: array, given array of numbers.
- :return: array, longest sequence of numbers that have a decreasing number of digits.
- """
- max_start_index = 0
- max_length = 0
- curr_start_index = 0
- curr_length = 0
- curr_digits = 0
- for _ in range(len(array)):
- if number_of_digits(array[_]) > 0:
- if curr_digits == 0:
- curr_digits = number_of_digits(array[_])
- curr_length = 1
- curr_start_index = _
- else:
- if curr_digits > number_of_digits(array[_]):
- curr_digits = number_of_digits(array[_])
- curr_length += 1
- else:
- curr_digits = number_of_digits(array[_])
- curr_length = 1
- curr_start_index = _
- if curr_length > max_length:
- max_start_index = curr_start_index
- max_length = curr_length
- if max_length > 0:
- index_array = []
- for _ in range(max_start_index, max_start_index + max_length):
- index_array.append(_)
- return index_array
- def test_second_assignment():
- assert second_assignment([1, 12, 123, 12, 1]) == [2, 3, 4]
- assert second_assignment(["test", "test2", "test 3231"]) is None
- assert second_assignment([1, 12, 123, -12, -1]) == [2, 3, 4]
- '''
- Testing
- '''
- test_is_square_number()
- test_first_assignment()
- test_number_of_digits()
- test_second_assignment()
- '''
- Meniu
- '''
- def user_interface():
- trigger = False
- array = []
- while not trigger:
- print("")
- print("Choose an action: ")
- print("")
- print("1. Input values")
- print("2. Print longest sequence of square numbers in given list of values.")
- print("3. Print longest sequence of numbers that have a decreasing number of digits.")
- print("4. Exit")
- print("")
- choice = str(input("Choice: "))
- print("")
- if len(choice) == 1 and choice.isdigit():
- if choice == '1':
- array = read_array()
- else:
- if choice == '2':
- if len(array) == 0:
- print("You must input some values first...")
- array = read_array()
- if len(array) >= 0:
- print("Original array: ")
- print(array)
- print("Index array: ")
- print(first_assignment(array))
- result = []
- for _ in first_assignment(array):
- result.append(array[_])
- print("Result: ")
- print(result)
- else:
- print("Original array: ")
- print(array)
- print("Index array: ")
- print(first_assignment(array))
- result = []
- for _ in first_assignment(array):
- result.append(array[_])
- print("Result: ")
- print(result)
- else:
- if choice == '3':
- if len(array) == 0:
- print("You must input some values first...")
- array = read_array()
- if len(array) >= 0:
- print("Original array: ")
- print(array)
- print("Index array: ")
- print(second_assignment(array))
- result = []
- for _ in second_assignment(array):
- result.append(array[_])
- print("Result: ")
- print(result)
- else:
- print("Original array: ")
- print(array)
- print("Index array: ")
- print(second_assignment(array))
- result = []
- for _ in second_assignment(array):
- result.append(array[_])
- print("Result: ")
- print(result)
- else:
- if choice == '4':
- trigger = True
- else:
- print("That's not a valid choice!")
- else:
- print("That's not a valid choice!")
- user_interface()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement