Posts

Showing posts from September, 2020

Hexadecimals to decimals

 Similar to the previous number systems with less than 10 digits, but this time there's more than 10. For this, the same thing happens, but you need to define the digits bigger than 10. For hexadecimals, that would be: a(10), b(11), c(12), d(13), e(14) and f(15). This time, I put the separate digits into the indexes of an array, and every time one of a, b, c, d, e or f came up, I instead inserted the corresponding numbers: def hexadecimal (number): numb_array = [] for x in range ( 0 , len (number)): if number[x] == "A" or number[x] == "a" : numb_array.append( 10 ) elif number[x] == "B" or number[x] == "b" : numb_array.append( 11 ) elif number[x] == "C" or number[x] == "c" : numb_array.append( 12 ) elif number[x] == "D" or number[x] == "d" : numb_array.append( 13 ) elif number[x] == "E" or number[x] ...

Binary to Decimal

Image
 Binary is a number system consisting of 2 numbers, 0 and 1. It works exactly like the most used number system, decimals. Binary to decimal conversion consists of multiplying the digit in the right column to the right power of 2. Something like this: So, the decimal number of the given binary number would equal to: which equals to 3757 base 10. So, what I did was take the binary number, change it into a string, put each individual digit into indexes in a list, reversed that list, then multiplied that digit by the index's power of 2. def binary (number): numb_array = [] for x in range ( 0 , len (number)): numb_array.append(number[x]) number = "" for x in range ( 0 , len (numb_array)): number += numb_array[ len (numb_array)-x- 1 ] output = 0 for x in range ( 0 , len (number)): output += int (number[x]) * ( 2 ** x) return output Ternary is the same thing, except that the 2 is replaced by 3: def ternary (number): numb_ar...

Uppercase and Lowercase reverser

 The program takes in a text file, and reverses the uppercase and lowercase letters. It figures out which is which by seeing if the letter is equal to the letter when it is changed to uppercase, and if it is the same, then write the lowercase version of the letter instead. starter = open ( "SCPtesting" ) output = open ( "reversed.txt" , "w" ) letter = 'a' while letter != '' : letter = starter.read( 1 ) if letter.upper() == letter: output.write(letter.lower()) elif letter.lower() == letter: output.write(letter.upper()) else : output.write(letter)

Palindrome

Image
 A palindrome is a word or sentence that is the exact same when read backwards. An example of this is 'racecar', 'live evil' and 'madam'. It's quite simple to check if a given phrase is a palindrome or not, by just comparing the first and last letters, then the second and second last letters, and so on, until you reach halfway. A slight challenge, is to only use one counter to do so.  words = input ( "Enter your word/sentence: " ) words_array = [] for x in range ( 0 , len (words)): words_array.append(words[x]) i = 0 palindrome = 0 while True : if words_array[i] == words_array[ len (words_array)-i- 1 ]: i += 1 palindrome = 1 else : palindrome = 0 break if i > len (words_array)/ 2 : break if palindrome == 0 : print ( "The provided word/sentence is not a palindrome" ) else : print ( "The provided word/sentence is a palindrome" ) Upon request, the input of words can ...

Hangman

 A simple game involving two players. One person thinks of a word or words, and the other tries to guess the letters that make up that word or words. The game I made requires two human players, one to type in the word or words, and the second to guess the letters. How I went about making the game is simple. Receive the words and separate them, letter by letter into an array, then create another array that's the exact same, but replaces all the letters with underscores, ignoring spaces. For example: word_array = ['T', 'H', 'I', 'S', ' ', 'T', 'E', 'S', 'T'] masked_array = ['_', '_', '_', '_', ' ', '_', '_', '_', '_'] Then when the letters are being guessed, you take the letter and see if it is in the word_array, if it is, then find the indexes the letters are in, then fill in the masked_array with the right letters. Continue doing this until...

5 Point Summary

 5 Point Summary is a set of numbers that provide information on a sequence of given numbers. It gives the minimum number, the first quartile, the second quartile / the median, the third quartile and the maximum. These values can be used to draw a box and whisker plot. Finding the minimum and maximum is easy, and you can find the first and third quartile by finding the median of all the numbers, then finding the median of the minimum to the median, then the median to the maximum. import math def summary (): enter = " " numbers = [] while enter != "" : enter = input ( "Enter your numbers(one by one): " ) if enter != "" : numbers.append( int (enter)) numbers.sort() sum = 0 for x in numbers: sum += x print (numbers) print ( "Sum:" , sum) average = sum/ len (numbers) print ( "Average:" , average) print ( "Minimum:" , numbers[ 0 ]) median ...

Separating the digits of an integer

 For example, if I were to input 12345, the program should return 1, 2, 3, 4, and 5.  There are two ways to do this, the 'cheat' way by changing the integer into a string and just taking the positions of the digits and fetching them that way. The other way is to divide it by powers of 10 and round it down to the nearest number, then minus that number from the original number, then take the resulting value, then minus all the number digits before that number: import math number = input ( "Please enter your number: " ) def stringway (input): input = str (input) digits_list = [] for x in range ( 0 , len (input)): digits_list.append( int (input[x])) print (digits_list) def integerway (input): input = int (input) digits_list = [] i = 1 while True : if i == 1 : temp = (input / 10 ** i) temp = math.floor(temp) temp = input - temp * 10 ** i digits_list.append( int (temp)) e...

Fibonacci's sequence

Image
  The following pattern is created if we are to visualize a never-ending sequence called Fibonacci's sequence. This sequence is created by starting with two numbers, 0 and 1. The following number is the two previous numbers added, therefor the third number in the sequence would be 1, and the fourth number in the sequence would be 2. In algebraic form:  Using a recursive loop that works with only three variables, it is quite simple to create in python: starter1 = 0 starter2 = 1 print (starter1) print (starter2) for x in range ( 0 , 20 ): temp = starter1 + starter2 starter1 = starter2 starter2 = temp print (temp)

Roman numerals to Arabic numerals

Image
The program will receive a number in Roman numerals, such as CLI, which is 151 in Arabic numerals.  Roman numerals use the above table to resemble to given numbers. If a smaller number is in front of a bigger number, it means to minus the small number from the bigger number. For example:  IV = 4, IX = 9 So all I needed to do was separate each letter and assign them their values, then added it to a list. Then, I just need to check if the value in position i was less then the position in i+1, which if it was, I just add the value of i+1 minus the value of i to the overall counter, and if it wasn't, I just add the value i to the overall counter: def roman_to_decimal (): roman = input ( "Enter your roman numeral: " ) roman_list = [] for x in range ( 0 , len (roman)): if roman[x] == "I" : roman_list.append( 1 ) elif roman[x] == "V" : roman_list.append( 5 ) elif roman[x] == "X" : ...

Link finder (wikipedia)

 Adding on to the web scraper that I made, this time I made a script that finds all the links in the given website. Since Wikipedia has a lot of links on their pages and I've already made a web scraping script for it, I made it for Wikipedia. What it does is it gets the singular big byte string containing all the html, decodes it into string and goes through it, looking for: <a href=". From then on, it adds whatever link is in between that and the next ". That gets all the links, and is placed in a list. Sometimes the links are shortened, as they go to different directories on the same website, for example: /wiki/Wikipedia:Featured_articles. For these, I just got the protocol and path from the initial url, and inserted the necessary ones in the beginning to make this link: https://en.wikipedia.org//wiki/Wikipedia:Featured_articles def get_links (): url = "https://en.wikipedia.org/wiki/Main_Page" protocol = "" for x in range ( 0 , len (ur...

Wikipedia Web Scraping

 I saw a post sometime ago on reddit about someone who created a website that tracks the spread of corona virus by attaining data from multiple different websites, and I wondered how he was able to grab data from websites. So, I tried it. It looks like you really just grab data by sorting through the html code. You just look through, and find information by using the id tags that the web creators used to lay out their website. I used a website that has an extremely consistent design that hasn't been changed for years: Wikipedia. I first tried getting the "Today's Featured Article" and printing the contents of it, and it wasn't hard, except for the fact that it displays the html code rather than text, but I haven't tried converting it to just text. The next was to use Wikipedia's special random link that would lead me to a random page, then make the program read the heading of the article, along with the main body of the article.  Using modules requests (fo...

Word counter

 It is very hard for a computer to actually count words in a given sentence. That's because words are made of different combinations of 26+ letters in the alphabet, so a clean definition of what a word is is hard to create. However, there's a roundabout way to find the number of words in a sentence, which is the count the spaces. Counting the spaces doesn't really count the words, but it has the same effect of counting the words, and I think that's what programming is about. It's like measuring 4 litres of water when you're computer can only calculate 5 litres and 3 litres of water. There's no 4 litre bottle, so you have to find a roundabout way to get 4 litres of water, using the available tools that are given to you. Of course if someone wanted to purposely mess with the working of the program, by inserting random spaces, it will not work properly. sentence = input ( "Please enter your sentence: " ) counter = 1 for x in range ( 0 , len (sentence)...

Guessing Game

A simple guessing game, where the computer picks a number between 1 and 100, allowing the player to guess a number, to which the game returns whether the number to be guessed is either bigger, smaller or equal to the guessed number. The game continues until the player has guessed the correct number.   import random print ( "This is a simple guessing game where you will guess a number between 1 and 100, and the computer will return whether the number is higher or lower." ) number = random.randint( 1 , 100 ) guess = - 1 while guess != number: guess = int ( input ( "Enter your guess: " )) if guess > number: print ( "The number is smaller!" ) elif guess < number: print ( "The number is larger!" ) elif guess == number: print ( "That is the number!" ) break

Caesar shift

 Caesar shift, aka Caesar cipher, is a simple encrypting algorithm that takes any letter from the alphabet and instead replaces it with the (default) 3rd next letter in the alphabet. The number can be changed. If the shift in number causes it to go past the last letter in the alphabet, it will simply start back from the start again. All that had to be done is to put the alphabet into a list, put the given sentence into a list, find the individual letter's indexes in the alphabet, and add 3 to that index to get the shifted letter, then append it to the output string. For the search of the letter from the input in the alphabet list, I imported and used my linear search from before: from Searches import linear alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alphabet_list = [] for x in range ( 0 , len (alphabet)): alphabet_list.append(alphabet[x]) get = input ( "Enter your sentence: " ).upper() output = "" for x in range ( 0 , len (get)): value = linear(al...

Scissors Paper Rock

 A simple game, that involves two players who individually pick between 3 options, Scissors, Paper and Rock. If both players pick the same item, it's a tie, and every other combination follows three rules: 1. Scissors beats paper 2. Paper beats rock 3. Rock beats scissors. All that needs to happen is to let the player choose between the three choices, let the computer pick between the three choices, and compare to get the outcome. There is one complication, which is that the computer doesn't know how to pick between three things. However, we can make the computer pick between 3 numbers, with a random number generator, which effectively makes the computer pick between three different choices. player = 2 while player != "S" and player != "s" and player != "P" and player != "p" and player != "R" and player != "r" : player = input ( "[S]cissors, [P]aper, [R]ock: " ) player.lower() computer = random.randint( 1...

Binary Search

 An efficient way to search through a large sorted array is the binary sort, which looks for a value by dividing up the array into twos. It finds the midpoint, determines if the value it is looking for is in either the part with the smaller numbers, or the part with the bigger numbers, then does the same thing again with said part. Need 3 counters for it, 'starter' to determine the starting index of the part you want to look through, the 'end' to determine the ending index of the part, and 'midpoint' to calculate the midpoint. def binary (numberlist , item): numberlist.sort() print ( "Sorted list: " , numberlist) starter = 0 end = len (numberlist) while True : midpoint = round ((starter + end)/ 2 ) if numberlist[midpoint] == item: return midpoint elif numberlist[midpoint] > item: end = midpoint elif numberlist[midpoint] < item: starter = midpoint if st...

Linear search

 The most common and simple method of searching through an array, which is to go through each element of an array and compare it to whatever you're searching for. Only needs one counter, 'i', to go through the array with. Breaks the loop after either the element is found or the end of the array has been reached. def linear (numberlist , item): for x in range ( 0 , len (numberlist)): if item == numberlist[x]: return x return None

Bubble sort

Image
  Bubble sort works by comparing two numbers right next to each other and swapping them if the bigger number is on the left. The simplest sort but also the most inefficient. You just need one counter to count through, and when you're doing the comparison, just do counter+1. You break the loop once there hasn't been a swap. def bubble(numblimit , amountlimit): numberlist = [] i = 0 while i < amountlimit: numberlist.append(random.randint( 1 , numblimit)) i += 1 print( "initial: " , numberlist) i = 0 swap = 1 while swap == 1 : swap = 0 for x in range( 0 , len(numberlist)- 1 ): if numberlist[x] > numberlist[x+ 1 ]: temp = numberlist[x] numberlist[x] = numberlist[x+ 1 ] numberlist[x+ 1 ] = temp swap = 1 break return numberlist

Selection sort

Image
  Selection sort works by selecting the smallest number in the "unsorted" part of the array, and then swapping it with the rightmost position of the "sorted" part of the array. So there's 3 counters that's needed for this, 'i', which goes through the unsorted array to find the smallest number, 'smallest_index', which records the index of the smallest number in the unsorted array, and 'marker' which marks the beginning of the unsorted array and where the smallest number of the unsorted list needs to go. It stops and exits when the marker reaches the end of the array. def selection (numblimit , amountlimit): numberlist = [] i = 0 while i < amountlimit: numberlist.append(random.randint( 1 , numblimit)) i += 1 print ( "initial: " , numberlist) i = 0 marker = 0 while marker < len (numberlist): smallest_index = marker while i < len (numberlist): if nu...

Insertion Sort

Image
 Insertion sort is a sorting algorithm that proceeds from left to right. It creates a "sorted" side by picking a number from the "unsorted" side of the array, then 'inserting' it into the correct sorted position in the "sorted" side of the array: I used two counters; "i" to go through the array and select the number to be sorted, then "j" to go through and compare the selected number to every number before it, i.e. the "sorted" list. All that's needed is to check if you're already at the leftmost number with "j", at which point you just need to insert the number to the first element. def insertion(numblimit, amountlimit):     numberlist = []     i = 0     while i < amountlimit:         numberlist.append(random.randint(1, numblimit))         i += 1     print("initial: ", numberlist)     i = 1     while i < len(numberlist):         j = i-1      ...