# Student Declaration # I [Name: JERIN SOYAN & ONEID Number: 48207055 ] declare that this is my own work and that # I have not used any code or logic from other people or from sources outside of the unit. # I understand that it is ok to look at COMP6010 videos and COMP6010 resources # and that researching how certain python operators / functions work is ok. # [x] <== put an x in here to indicate you have read and agree to the above statements. def is_number(value): """ (12.5 marks) Given a variable, return True if the variable is of type int or float, False otherwise. """ return isinstance(value, int) or isinstance(value, float) def process_number(n: int): """ (12.5 marks) Given an int, convert each group of two digits into their associated ASCII character value (hint: chr(int) function) and return a string with all the characters. You may assume that the number has an even number of digits and is a positive number. Example: 58979943 -> 58 97 99 43 -> ":ac+" You must perform type checking to ensure the inputs are of correct type. If the input is not of correct type, your function should return None """ if not isinstance(n, int): return None n_str = str(n) ascii_chars = [chr(int(n_str[i:i+2])) for i in range(0, len(n_str), 2)] return ''.join(ascii_chars) def ascii_sum_is_even(s: str): """ (12.5 marks) Given a string, find the sum of ascii values of each character and return True if the sum is even, False otherwise. You can find the ascii value of a character by using the ord() function. You must perform type checking to ensure the inputs are of correct type. If the input is not of correct type, your function should return None """ if not isinstance(s, str): return None ascii_sum = sum(ord(char) for char in s) return ascii_sum % 2 == 0 def sum_two_lowest(s: str): """ (12.5 marks) Given a string, find the two characters with the two lowest ascii values in the string and return the sum of their ascii values """ ascii_values = [ord(char) for char in s] sorted_ascii_values = sorted(ascii_values) return sorted_ascii_values[0] + sorted_ascii_values[1] def ordered_by_lowest(a, b, c): """ (12.5 marks) Given three strings, calculate the sum of the two lowest ascii values for each string and return a string containing all three strings combined in ascending order based on the sum of their lowest ascii values You must complete this without sorting or lists. You must perform type checking to ensure the inputs are of correct type. If the input is not of correct type, your function should return None """ if not isinstance(a, str) or not isinstance(b, str) or not isinstance(c, str): return None def get_lowest_ascii_sum(string): ascii_values = [ord(char) for char in string] return sum(sorted(ascii_values)[:2]) combined_strings = [(get_lowest_ascii_sum(a), a), (get_lowest_ascii_sum(b), b), (get_lowest_ascii_sum(c), c)] combined_strings.sort() return ''.join(string for _, string in combined_strings) def blat(a, b, c) -> bool: """ (12.5 marks) Given three variables, return based on the following True if ANY of the following apply: - All variables are numbers (ints or floats) - At least two of the variables are even numbers - At least two of the variables are strings that are longer than 5 characters (exclusive) False otherwise. """ def is_even(num): return isinstance(num, int) and num % 2 == 0 def is_long_string(string): return isinstance(string, str) and len(string) > 5 if (isinstance(a, (int, float)) and isinstance(b, (int, float)) and isinstance(c, (int, float))) or \ (is_even(a) and is_even(b)) or \ (is_even(a) and is_long_string(b)) or \ (is_even(a) and is_long_string(c)) or \ (is_even(b) and is_long_string(a)) or \ (is_even(b) and is_long_string(c)) or \ (is_even(c) and is_long_string(a)) or \ (is_even(c) and is_long_string(b)): return True else: return False def get_information(n: int): """ (12.5 marks) Given an int, calculate the number of even digits, odd digits and prime digits. Return this information using the string format provided below. The number ____ contains ____ even digits, ____ odd digits and ____ prime digits For example if the number was 1838359, then the function should return the string: The number 1838359 contains 2 even digits, 5 odd digits and 3 prime digits For example if the number was 19, then the function should return the string: The number 19 contains 0 even digits, 2 odd digits and 0 prime digits You must perform type checking to ensure the inputs are of correct type. If the input is not of correct type, your function should return None You must complete this question without performing any string conversions """ if not isinstance(n, int): return None def is_prime(num): if num < 2: return False for i in range(2, int(num ** 0.5) + 1): if num % i == 0: return False return True even_count = odd_count = prime_count = 0 num = n # Corrected variable name here while num > 0: digit = num % 10 if digit % 2 == 0: even_count += 1 else: odd_count += 1 if is_prime(digit): prime_count += 1 num //= 10 # Corrected variable name here return f"The number {n} contains {even_count} even digits, {odd_count} odd digits and {prime_count} prime digits" def what(s: str): """ (12.5 marks) Given a string, perform the following operations: - Duplicate any vowels in the string. - Keep only the first occurrence of each non-vowel character in the string Note: the character b is not the same as the character B For example if the string was: abbayeioub then the string should become: aabaayeeiioouu Return the resulting string. You must perform type checking to ensure the inputs are of correct type. If the input is not of correct type, your function should return None """ if not isinstance(s, str): return None vowels = "aeiouAEIOU" new_string = '' for char in s: if char in vowels: new_string += char * 2 elif char not in new_string: new_string += char return new_string # Example usage: print(is_number(5)) print(process_number(58979943)) print(ascii_sum_is_even("Hello")) print(sum_two_lowest("apjkdic")) print(ordered_by_lowest("abc", "oojf", "0anv")) print(blat(1, 5, 4.8)) print(get_information(192391)) print(what("abbayeioub"))
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |