Posts

Showing posts from October, 2020

Binary subtraction

 Computer subtract two numbers by using a trick called two's complement. For example, if you wanted to do A - B with A and B as binary integers, what computer actually do is A + F(B) + 1, with F() being a function that handles two's complement. Two's complement works by reversing the digits of the binary number. If A = 1101011, and B = 101, first of all, you make B the same length as A by putting 0s in the front, B = 0000101. Then, you reverse the digits, so that F(B) = 1111010. Then you add one to F(B),  B = 1111011. Then you add those two numbers together, and ignore the left-most digit. A + F(B) + 1 = 1 1100110. This leaves us with 1100110, which should be equal to A - B: 1101011 = 107 101 = 5 1100110 = 102  107 - 5 = 102 number = input ( "Enter your binary digit: " ) array = [] for x in range ( 0 , len (number)): if number[x] == "1" : array.append( 0 ) elif number[x] == "0" : array.append( 1 ) number = input ( "...

Nim game

 The nim game requires 2 players, and 20 stones. The players take turns removing 1 to 3 stones at a time, and the person to remove the last stones is the winner.  There is a method of playing that allows you to win the game if you go second. Whatever the number of stones the opponent takes, take the number of stones so that the stones taken by you and your opponent for that exchange of turns become 4. That way, it will eventually reach 4 stones left on your opponents turn, meaning that no matter how many stones they take, you will win. For example: 20(starting) 1 - 18(2) 2 - 16(2) 1 - 13(3) 2 - 12(1) 1 - 9(3) 2 - 8(1) 1 - 7(1) 2 - 4(3)(checkmate) stones = 20 print (stones , "stones" ) while True : player1 = int(input( "Player1: enter the number of stones to take, 1~3: " )) stones -= player1 print( "Remaining stones:" , stones) if stones <= 0 : print( "player1 wins" ) break player2 = int(input( "Player2:...

Download Calculator

 I was downloading Overwatch again, and blizzard doesn't have an estimated download timer, and the online ones are kind of weird with the bits and bytes and the different types, so I just made a simple one so I don't have to rack my brain everytime. size = float ( input ( "Enter the download size in gigabytes: " )) speed = float ( input ( "Enter your download speed in megabytes per second: " )) size = size* 1000 time = size/speed print (time , "seconds" ) time = time/ 60 print ( "Or" ) print (time , "minutes" ) time = time/ 60 print ( "Or" ) print (time , "hours" )

Random license plates

 The simple program makes a simple 3 letter and 3 digit license plate, in the form LLL-DDD. It just picks 3 random letters from a list of letters, then 3 random digits. def license_plate (): alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" i = 0 alphabet_array = [] while i != len (alphabet): alphabet_array.append(alphabet[i]) i += 1 output = "" for x in range ( 0 , 3 ): output += alphabet[random.randint( 0 , 25 )] output += "-" for x in range ( 0 , 3 ): output += str (random.randint( 0 , 9 )) return output

Sound every hour

 Using the previous two program's components, I made a program that gives me a notification every hour that also tells me what time it is. This will give me an idea of how much time is passing. I'll have this open in a cmd window. from datetime import datetime import win10toast toast = win10toast.ToastNotifier() while True : time = datetime.now().strftime( "%M:%S" ) hour = datetime.now().strftime( "%H" ) if int (hour) > 12 : hour = str ( int (hour) - 12 ) if time == "00:00" : toast.show_toast( "Hourly notification" , "The time is " + hour + ":00" )

Better alarm

 I was thinking that the last program I made wasn't a good method of doing it, so I changed it. This time around, the program takes in a time in 24 hour format, in the form "HH:MM:SS". It then adds it to a list of those values, so that it can go through them and see if the current time is a time where an alarm needs to go off. If it is, it shows the notification, then removes that value from the list. If the list is empty, the program terminates. The current program doesn't allow for multiple inputs into the alarm's list, because the CLI will stop the program's progress if not given an input. Basically, the program is set to be ready for graphics integration.  from datetime import * import win10toast alarm_time = [] toast = win10toast.ToastNotifier() alarm_time.append( input ( "Enter the time for the alarm, in the form HH:MM:SS in 24 hour time: " )) print (alarm_time[ 0 ]) while True : time = datetime.now().strftime( "%H:%M:%S" ) i ...

Reminder

 Simple program that takes in a certain amount of hours, minutes and seconds, then a description of the notification to ping the user with after that amount of time has passed. Makes use of the modules time and the previous module used, win10toast. import time from win10toast import ToastNotifier print ( "This program will send a windows notification after a given amount of time." ) hours = int ( input ( "Enter hours: " )) minutes = int ( input ( "Enter minutes: " )) seconds = int ( input ( "Enter seconds: " )) description = input ( "Enter your description of the reminder: " ) toast = ToastNotifier() total_time = seconds + minutes * 60 + hours * 3600 time.sleep(total_time) toast.show_toast( "Reminder" , description , icon_path = r"C:\Users\Conan\Desktop\rand\36466-200.ico" )

Toast notifications

Image
 The previous project required windows notifications, so that it can ping me with new manga releases. I couldn't get the log in thing from the previous project working, but I'll at least get this running for now. The title and description part was simple enough, as it's just strings that I can change into whatever I want. The icon was a little more difficult as I have to enter the path of the image, and the image specifically has to be in ico file format. The next thing to worry about is having the program run in the background, without an active window. Looked it up, and apparently I have to place the shebang in the front so python knows which version and virtual environment it should use, and some other thing I'll do tomorrow. #!/usr/bin/env python3 import time from win10toast import ToastNotifier toast = ToastNotifier() while True : toast.show_toast( "Sample notificaiton" , "sample description" , icon_path = r"C:\Users\Conan\Desktop\rand\...

Mangadex

 I use mangadex pretty much daily to read manga and stuff, and recently I found out that I constantly go on it every now and then to check if there was an update on anything I read. So I decided to write a script that goes and checks my following list and gives me a notification when there is an update. It didn't work out. Using the requests module again, I tried just doing it off the bat, which didn't work because it required me to log in first of all. I thought that I'll just log in and that'll be that. How foolish I was.  So, usually, if you want to log in, you send a post request to the login page of said website, which contains the username and password of the account you want to log in as. You do this by creating a session, which will retain the cookies used for logging in, which I wanted so that I can refresh the page every few minutes without logging in again. So you go to the website and find the <input> tag's names and put them in a dictionary like s...

Find (Ctrl + F)

Just like the find function in webpages and text editors, this code takes a file, and a given string to find in that file. Once it has been found, it returns the line it was found in, and the line number of that line. If nothing is found, nothing is returned, and it also returns multiple results if there are multiple times where that string shows up. def find (): fo = open ( input ( "Enter the name of your file: " ) , "r+" ) context = input ( "Enter the search paramaters: " ) lines = fo.readlines() for x in range ( 0 , len (lines)): if context in lines[x]: print ( "The given is in the file, within the line, " , lines[x]) print ( "In line number" , x- 1 )

Comparing two file's characters

Using the previous code that I made, this compares the characters used in both files and returns which characters one has that the other does not.  def compare (): file1 = characters() file2 = characters() in_file1_not_file2 = [] in_file2_not_file1 = [] for x in file1: if x not in file2: in_file1_not_file2.append(x) for x in file2: if x not in file1: in_file2_not_file1.append(x) print ( "Characters in first file but not in second file:" , in_file1_not_file2) print ( "Characters in second file but not in first file:" , in_file2_not_file1)

Get Characters

 A program that returns every character used in a text file. It ignores if the letter is in uppercase or not.  def characters (): fo = open ( input ( "Enter the name of your file: " ) , "r+" ) character_list = [] character = "__" while character != "" : character = fo.read( 1 ) try : character = character.lower() except : pass if character not in character_list and character != " \n " and character != "" : character_list.append(character) return character_list

Clap sequence

 The clap sequence is a sequence that generates a list using the previous list's numbers. It starts with [1], and then the frequency of the same numbers in a row is added, followed by the actual number itself. Therefore, the next sequence would be [1, 1], as there is one 1, then it would be [2, 1], as there are two 1s, then [1, 2, 1, 1], as there is one 2 and one 1. array = [ 1 ] for sentinel in range ( 0 , 10 ): count = 1 new_array = [] for x in range ( 0 , len (array)): number = array[x] try : if array[x] == array[x+ 1 ]: count += 1 else : new_array.append(count) new_array.append(number) count = 1 except IndexError as e: new_array.append(count) new_array.append(number) array = new_array print (array) This is the outcome: [1, 1] [2, 1] [1, 2, 1, 1] [1, 1, 1, 2, 2, 1] [3, 1, 2, 2, 1, 1] [1, 3, 1, 1, 2, 2, 2, 1] [1, 1, 1, 3, 2, 1...

Mimicking a keyboard

Using the pynput module, I tried to make a 'macro' by just making the computer type in stuff. Unlike printing stuff or putting things into a file, this will allow me to 'type' at a very fast speed, and to be honest I was just messing around with it. First thing I did was make it type out a given string, then I moved on to typing out a given file. I tested with stuff like game config files, and find out that if the file is too long, it'll have hiccups near the end of the file. And I don't mean like small mistakes, it just doesn't do it properly. I tested it twice to see if the same mistakes occur, and they were different: Last line of test1: nb   tl   .gitttonwii AAAccls Last line of test2: sAA I went and found some ASCII art and tried to paste it into Discord and Minecraft, but both were unfruitful. It would've worked for discord, even though it had message sending limits, if I just slowed the sending by like half a second it was fine, except for the fac...