# Lab Activity 5

May 5th, 2022
862
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def binaryCounter(x):
2.     if type(x) is not int:
3.         raise TypeError("Input an integer only")
4.     numbers = []  # list for initial numbers
5.     binary = ['0']  # list for binary, with 0 automatically placed due to python treating 0 as empty
6.     binaryNegative = []
7.     y = 0
8.     if x > 0:
9.         while y != x:  # first counts the number from 0 up to the inputted number
10.             y = y + 1
11.             numbers.append(y)  # appends the numbers into the number list
12.         for digit in numbers:  # computes for the binary for each number in the number list
13.             convertedBinary = bin(digit)[2:]
14.             binary.append(str(convertedBinary))  # appends the converted binary numbers into the binary list
15.         return binary
16.     else:
17.         return binaryNegative
18.
19.
20. def toPalindrome(x):  # for palindromes
21.     if not (type(x) is str or type(x) is int):
22.         raise TypeError("Input a string or integer only")
23.     # this uses the slice notation of lists, from the last character up to the first, counting down by 1
24.     d = ', '
25.     return str(x) + (str(x)[::-1]) + d + (str(x)[::-1]) +str(x)
26.
27.
28. def numtomaxn(x):  # to compute the number in descending order
29.     y = []  # list to contain the converted numbers
30.     y[:] = str(x)  # this part is to convert the inputted integer into a list per digit
31.     # descending sort from previous experiment
32.     for numbers in range(0, len(y) - 1):
33.         for innerNumber in range(0, len(y) - 1 - numbers):
34.             if y[innerNumber] < y[innerNumber + 1]:
35.                 y[innerNumber], y[innerNumber + 1] = y[innerNumber + 1], y[innerNumber]
36.     for i in y:
37.         print(int(i), end='')  # prints out the sorted integer
38.     return ''
39.
40.
41. def bitwise_NAND(x, y):  # to compute a bitwise NAND operation on a binary list
42.     # declaration of initial lists
43.     first = []
44.     second = []
45.     final = []
46.     first[:] = x
47.     second[:] = y
48.     for l1, l2 in zip(first, second):
49.         if l1 == "1":
50.             if l2 == '0':
51.                 final.append("1")
52.             elif l2 == '1':
53.                 final.append("0")
54.         elif l1 == "0":
55.             if l2 == '1':
56.                 final.append("1")
57.             elif l2 == '0':
58.                 final.append("1")
59.     for i in final:  # converts the list into a single string
60.         print(int(i), end='')
61.     return ''
62.
63.
64. print("Binary")
65. print("-------------------------")
66. print(binaryCounter(1))
67. print(binaryCounter(2))
68. print(binaryCounter(3))
69. print(binaryCounter(-3))
70. print(binaryCounter(6))
71. print(binaryCounter(4))
72. print(binaryCounter(5))
73. print(binaryCounter(11))
74. print(binaryCounter(13))
75. print(binaryCounter(10))
76. print(binaryCounter(15))
77. print(binaryCounter(20))
78. print(" ")
79. print("Palindrome")
80. print("-------------------------")
81. print(toPalindrome(567))
82. print(toPalindrome("Hello"))
83. print(toPalindrome("192.168"))
84. print(toPalindrome(101))
85. print(toPalindrome("Pneumonoultramicroscopicsilicovolcanoconiosis"))
86. print(toPalindrome('Bruh'))
87. print(toPalindrome('All work and no play makes Jack a dull boy'))
88. print(toPalindrome('The fog was as thick as pea soup. This was a problem.'))
89. print(toPalindrome(123456789))
90. print(toPalindrome("my mama don't like you but she likes everyone"))
91. print(toPalindrome('Ruby Kurosawa'))
92. print(toPalindrome('Chika Takami'))
93. print(" ")
94. print("NumToMaxN")
95. print("-------------------------")
96. print(numtomaxn(576233))
97. print(numtomaxn(31415926535))
98. print(numtomaxn(2718281828))
99. print(numtomaxn(987654321))
100. print(numtomaxn(6390123456))
101. print(numtomaxn(123987654))
102. print(numtomaxn(348597983475937589356))
103. print(numtomaxn(12390470812370570934))
104. print(numtomaxn(32498790178238914718947892))
105. print(numtomaxn(32426))
106. print(numtomaxn(9879837976))
107. print(numtomaxn(2146878921232))
108. print(numtomaxn(59789712213213))
109. print(numtomaxn(32166897889788465))
110. print(numtomaxn(3215465489786462))
111. print(numtomaxn(987654312165498))
112. print(numtomaxn(12454648798874))
113. print(numtomaxn(51546897879545121))
114. print(numtomaxn(97423546))
115. print(numtomaxn(879545121))
116. print(" ")
117. print("bitwise_NAND operation")
118. print("-------------------------")
119. print(bitwise_NAND("0010011011", "1010000011"))
120. print(bitwise_NAND("11100000", "00011111"))
121. print(bitwise_NAND("1010101", "1111000"))
122. print(bitwise_NAND("1", "0"))
123. print(bitwise_NAND("0110110010010010", "1111110000110111"))
124. print(bitwise_NAND("010100101010", "100101110101"))
125. print(bitwise_NAND("1001001001", "0110011100"))
126. print(bitwise_NAND("1111011011", "1010011100"))
127. print(bitwise_NAND("1010011010111010000110010", "0001111000001001111000110"))
128. print(bitwise_NAND("0101001100011100110010101", "1011100010100000110010001"))