Word Frequency Challenge

--Originally published at Carolina's Blog Site

So, the other day Ken gave us a challenge: Using a file full of words, create a program that will count the number of times each word is repeated (break the text into words and count the frequencies of them).

He told us this sorta program can be used for analyzing encrypted data. A program can count, for example, what the most repeated letter in a file is, and then that can be used to figure out what language it is written in (since most languages have a different most used letter), if that makes any sense.

Anyway, I thought that was pretty cool.

So this took me awhile. It actually covers a lot of topics we’ve seen: working with files, dictionaries, functions, if-else statements, user input, etc. Check it out! https://github.com/cpvp20/frequency_challenge/blob/master/frequency.py


Files and files and files.

--Originally published at Carolina's Blog Site

Opening Files

You can use Python to read and write the contents of files. Before a file can be edited, it must be opened, using the open function.

myfile = open(“filename.txt”)

The first argument of the open function is the path to the file (however, if the file is in the same directory as the program, you can specify only its name).

The second argument is the mode used to open a file.
“r” means open in read mode, which is the default.
“w” means write mode, for rewriting the contents of a file.
“a” means append mode, for adding new content to the end of the file.

Adding “b” to a mode opens it in binary mode, which is used for non-text files (like image and sound files).

Image result for meme binary
Btw, if you don’t recognize this, you really need to watch Pulp Fiction.

Once a file has been opened and used, you should close it.
This is done with the close method of the file object.

file.close()

Reading Files

The contents of a file that has been opened in text mode can be read using the read method. Here are some other methods:

>>> f.read()
'This is the entire file.\n'
>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
Or you can create a for loop:
>>> for line in f:
...     print(line, end='')

Other stuff

To write to files you use the write method, which writes a string to the file.

You should know, when a file is opened in write mode, the file’s existing content is deleted.

Image result for what why meme

Another way of working with files is using with statements. This creates a temporary variable (often called f), which is only accessible in the indented block of the with statement.with open(“filename.txt”) as f:

print(f.read())
The file is automatically closed at the end of the with statement, even if exceptions occur within it; this can save you a lot of problems.

Hope this was helpful!!

sources:

https://docs.python.org/3/tutorial/inputoutput.html

https://www.memecenter.com/fun/2363511/i-amp-039-m-losing-my-voice/comments


Frequency challenge.

--Originally published at Carolina's Blog Site

Hey kids, been busy for awhile, but I’m back.

im back friends matt leblanc

So, the other day Ken gave us a challenge: Using a file full of words, create a program that will count the number of times each word is repeated (break the text into words and count the frequencies of them).

He told us this sorta program can be used for analyzing encrypted data. A program can count, for example, what the most repeated letter in a file is, and then that can be used to figure out what language it is written in (since most languages have a different most used letter), if that makes any sense.

Anyway, I thought it was pretty awesome.

So this took me awhile, but after getting some help from my bestie the internet, I did it. Check it out!

K, bye.

 

gif from https://giphy.com/gifs/friends-matt-leblanc-im-back-FrlXUwX4Gidiw

 

 


Function: GCD (Giant Chocolate Donut)

--Originally published at Carolina's Blog Site

So today I made a function that asks the user for 2 integers and then displays the Greatest Common Denominator, which is the largest number that divides both of them without leaving a reminder. I realized there’s several ways to do this, but I think this one’s pretty simple. It took me awhile to get it though. I just couldn’t make sense of it! Thank goodness I have fellow programmer friends who can explain theses sorta things to me?


Why you should care about functions.

--Originally published at Carolina's Blog Site

A function is a named sequence of statements that performs an operation/computation. Functions may or may not take arguments (which are like the ingredients that change depending on what result you want to achieve in that particular occasion).

To define a function,

  • you write def followed by the name of the function,
  • then you write the parameters/arguments in () and you end with a “:”
  • In the body of the function, which has to be indented, you type the order of the statements you want that function to do.
  • To finish off a function, type return and specify what the function will return. If you don’t want to return anything, just leave the return line blank with (). Any code after the return statement will not happen.

Later, you can “call” the function by its name, and just type the parameters for that occasion. I know, awesome!

Side note:

  • Parameters can have a default value, for example: (parameter1, parameter2, parameter3=55).
  • If a function is a thing and parameters can be anything…then functions can also be used as parameters of other functions.

 

pic souce: http://www.relatably.com/m/functional-programming-memes