commit 24600885b9a049ce13de506bd255ea04e91dda1b Author: github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon Nov 13 12:47:53 2023 +0000 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/README.md b/README.md new file mode 100644 index 0000000..7f0f854 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Project 02 - Python test generator + +### About the Project + +This project has the goal of writing a search based automated test generator for Python. +It is part of the Knowledge Search & Extraction - 2023 course from the Università della Svizzera italiana. + +In this repository, you can find the following files: +- benchmark folder: which contains the benchmark of functions under test to be instrumented + +Note: Feel free to modify this file according to the project's necessities. + + + diff --git a/benchmark/anagram_check.py b/benchmark/anagram_check.py new file mode 100644 index 0000000..842c269 --- /dev/null +++ b/benchmark/anagram_check.py @@ -0,0 +1,15 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/anagram_check.py + +# function to check if two strings are +# anagram or not +def anagram_check(s1: str, s2: str) -> bool: + if len(s1) == 1 and len(s2) == 1: + return s1 == s2 + if len(s1) != len(s2): + return False + + # the sorted strings are checked + if ''.join(sorted(s1)) == ''.join(sorted(s2)): + return True + else: + return False diff --git a/benchmark/caesar_cipher.py b/benchmark/caesar_cipher.py new file mode 100644 index 0000000..eff4b5c --- /dev/null +++ b/benchmark/caesar_cipher.py @@ -0,0 +1,25 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/caesar_cipher.py + + +def encrypt(strng: str, key: int) -> str: + assert 0 < key <= 94 + encrypted = '' + for x in strng: + indx = (ord(x) + key) % 256 + if indx > 126: + indx = indx - 95 + encrypted = encrypted + chr(indx) + return encrypted + + +def decrypt(strng: str, key: int) -> str: + assert 0 < key <= 94 + decrypted = '' + for x in strng: + indx = (ord(x) - key) % 256 + if indx < 32: + indx = indx + 95 + decrypted = decrypted + chr(indx) + return decrypted + + diff --git a/benchmark/check_armstrong.py b/benchmark/check_armstrong.py new file mode 100644 index 0000000..a28715d --- /dev/null +++ b/benchmark/check_armstrong.py @@ -0,0 +1,18 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/check_armstrong.py + +def check_armstrong(n: int) -> bool: + assert n >= 0 + if n == 0 or n == 1: + return True + if n <= 150: + return False + t = n + sum = 0 + while t != 0: + r = t % 10 + sum = sum + (r * r * r) + t = t // 10 + if sum == n: + return True + else: + return False diff --git a/benchmark/common_divisor_count.py b/benchmark/common_divisor_count.py new file mode 100644 index 0000000..236bd5d --- /dev/null +++ b/benchmark/common_divisor_count.py @@ -0,0 +1,27 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/common_divisor_count.py + +""" +The function takes two integers as input and return the number of common divisors of +that pair +""" + + +def cd_count(a: int, b: int) -> int: + if a == 0 or b == 0: + return 2 + a = (-1 * a if a < 0 else a) + b = (-1 * b if b < 0 else b) + + result = 0 + while a != 0: + c = a + a = b % a + b = c + + for i in range(1, int((b ** 0.5) + 1)): + if b % i == 0: + if int(b / i) == i: + result = result + 1 + else: + result = result + 2 + return result diff --git a/benchmark/exponentiation.py b/benchmark/exponentiation.py new file mode 100644 index 0000000..5ced970 --- /dev/null +++ b/benchmark/exponentiation.py @@ -0,0 +1,24 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/exponentiation.py + +def exponentiation(baseNumber: int, power: int) -> float: + assert not (baseNumber == 0 or power <= 0) + + answer = None + + if power > 1: + halfAnswer = exponentiation(baseNumber, power//2) + answer = halfAnswer * halfAnswer + + if power%2 == 1: + answer *= baseNumber + + elif power == 1: + answer = baseNumber + + elif power == 0: + answer = 1 + + else: # negative power + answer = 1 / exponentiation(baseNumber, abs(power)) + + return answer diff --git a/benchmark/gcd.py b/benchmark/gcd.py new file mode 100644 index 0000000..0228d6b --- /dev/null +++ b/benchmark/gcd.py @@ -0,0 +1,15 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/common_divisor_count.py + +def gcd(a: int, b: int) -> int: + assert a > 0 and b > 0 + if a == 1 or b == 1: + return 1 + if a == b: + return a + if b > a: + a, b = b, a + while b != 0: + temp = b + b = a % b + a = temp + return a diff --git a/benchmark/longest_substring.py b/benchmark/longest_substring.py new file mode 100644 index 0000000..0abea14 --- /dev/null +++ b/benchmark/longest_substring.py @@ -0,0 +1,28 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/longest_substring.py + +# Create an algorithm that prints the longest substring of s in which +# the letters occur in alphabetical order. For example, if +# s = 'azcbobobegghakl', then your program should print: +# Longest substring in alphabetical order is: beggh + +# In the case of ties, print the first substring. +# For example, if s = 'abcbcd', then your program should print: +# Longest substring in alphabetical order is: abc + + +def longest_sorted_substr(s: str) -> str: + count = 0 + max_count = 0 + end_position = 0 + for char in range(len(s) - 1): + if (s[char] <= s[char + 1]): + count += 1 + if count > max_count: + max_count = count + end_position = char + 1 + else: + count = 0 + start_position = end_position - max_count + return s[start_position:end_position+1] + + diff --git a/benchmark/rabin_karp.py b/benchmark/rabin_karp.py new file mode 100644 index 0000000..486547e --- /dev/null +++ b/benchmark/rabin_karp.py @@ -0,0 +1,46 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/rabin_karp.py + + +# Rabin Karp Algorithm in python using hash values + + +def rabin_karp_search(pat: str, txt: str) -> list: + assert len(pat) <= len(txt) + + # d is the number of characters in input alphabet + d = 2560 + q = 101 + + M = len(pat) + N = len(txt) + i = 0 + j = 0 + + p = 0 + t = 0 + h = 1 + + for i in range(M - 1): + h = (h * d) % q + + for i in range(M): + p = (d * p + ord(pat[i])) % q + t = (d * t + ord(txt[i])) % q + + found_at_index = [] + for i in range(N - M + 1): + if p == t: + for j in range(M): + if txt[i + j] != pat[j]: + break + + j += 1 + if j == M: + found_at_index.append(i) + + if i < N - M: + t = (d * (t - ord(txt[i]) * h) + ord(txt[i + M])) % q + if t < 0: + t = t + q + + return found_at_index diff --git a/benchmark/railfence_cipher.py b/benchmark/railfence_cipher.py new file mode 100644 index 0000000..56dedd7 --- /dev/null +++ b/benchmark/railfence_cipher.py @@ -0,0 +1,73 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/railfence_cipher.py + + +def railencrypt(st: str, k: int) -> str: + assert k > 1 + c = 0 + x = 0 + m =[[0] * (len(st)) for i in range(k)] + for r in range(len(st)): + m[c][r] = ord(st[r]) + if x == 0: + if c == (k-1): + x = 1 + c -= 1 + else: + c += 1 + else: + if c == 0: + x = 0 + c += 1 + else: + c -= 1 + + result = [] + for i in range(k): + for j in range(len(st)): + if m[i][j] != 0: + result.append(chr(m[i][j])) + return ''.join(result) + + +def raildecrypt(st: str, k: int) -> str: + assert k > 1 + c , x = 0 , 0 + m =[[0] * (len(st)) for i in range(k)] + for r in range(len(st)): + m[c][r] = 1 + if x == 0: + if c == (k-1): + x = 1 + c -= 1 + else: + c += 1 + else: + if c == 0: + x = 0 + c += 1 + else: + c -= 1 + result = [] + c , x = 0 , 0 + for i in range(k): + for j in range(len(st)): + if m[i][j] == 1: + m[i][j] = ord(st[x]) + x += 1 + for r in range(len(st)): + if m[c][r] != 0: + result.append(chr(m[c][r])) + if x == 0: + if c == (k-1): + x = 1 + c -= 1 + else: + c += 1 + else: + if c == 0: + x = 0 + c += 1 + else: + c -= 1 + return ''.join(result) + diff --git a/benchmark/zellers_birthday.py b/benchmark/zellers_birthday.py new file mode 100644 index 0000000..1c62b38 --- /dev/null +++ b/benchmark/zellers_birthday.py @@ -0,0 +1,52 @@ +# Based on https://github.com/AllAlgorithms, python/algorithms/math/zellers_birthday.py + + +def zeller(d: int, m: int, y: int) -> str: + assert abs(d) >= 1 + assert abs(m) >= 1 + assert 0 <= abs(y) <= 99 or 1000 <= abs(y) <= 3000 + + d = abs(d) + m = abs(m) + y = abs(y) + if d > 31: + d = d % 31 + 1 + if m > 12: + m = m % 12 + 1 + if y < 100 and y < 23: + y = 2000 + y + if y < 100 and y >= 23: + y = 1900 + y + + days = { + '0': 'Sunday', + '1': 'Monday', + '2': 'Tuesday', + '3': 'Wednesday', + '4': 'Thursday', + '5': 'Friday', + '6': 'Saturday' + } + + # m = int(bday[0] + bday[1]) + # d = int(bday[3] + bday[4]) + # y = int(bday[6] + bday[7] + bday[8] + bday[9]) + + if m <= 2: + y = y - 1 + m = m + 12 + c = int(str(y)[:2]) + k = int(str(y)[2:]) + + t = int(2.6 * m - 5.39) + u = int(c / 4) + v = int(k / 4) + x = d + k + z = t + u + v + x + w = z - (2 * c) + + f = round(w % 7) + + for i in days: + if f == int(i): + return days[i]