Challenge completed!

--Originally published at Hector Martinez Alcantara

Here’s my code of the second challenge from the class TC101.

It consist in sorting some numbers, an user type some numbers separated by coma in the terminal, and then the program do the bubble sort to re-order the numbers from smallest to largest.

First I read the numbers, then I compare two positions, to check if the number is higher than the previous number and if it’s the case, they change positions, doing this iteration length – 1 times.

That’s called bubble sort.

text=input("Type some numbers separated by coma\n")
numbers=text.split(',')
print("In disorder")
for number in numbers:
 print(number)
length=len(numbers)-1
for iteration in range(0,(length)):
 for position in range(0,(length)):
 if numbers[position] > numbers[position+1]:
 change=numbers[position]
 numbers[position]=numbers[position+1]
 numbers[position+1]=change
print("In order")
for number in numbers:
 print(number)

Thanks for reading.

 


Challenge TC101 Frequency Dictionary

--Originally published at Programming

The first challenge posted by ken is the next one:

Using a file full of words, create a program that counts how many times each word is repeated in the file. We are going to do first some functions that’ll help us throughout this process. First we’ll create a function read_from_file which will basically open a file and read from it every word, storing them in a list. This function returns a list of words.

def read_from_file(file):
    """
    :param file: The file words are going to be read from
    :return: a list with the words inside the file
    """
    lista = []
    try:
        f = open(file, 'r')
        for line in f:
            line = line.strip('\n')  # Get rid of the \n at the end of each line
            lista += line.split(" ")  # get every word of the line and store it inside a list
        f.close()
    except IOError:
        print("Cannot open " + file)
    return lista

Now we are going to create a function that counts the frequencies of the words inside a list by storing it in a dictionary. The dictionary keys will be the words and the values will be the number of times each word appears in the list. This function returns a dictionary with frequencies.

def count_frequency(words):
    """
    :param words: List of words
    :return: A dictionary with frequencies of words
    """
    frequency_words = {}
    for word in words:  # iterate over the list
        # If the word is not already inside the dictionary's keys, create it
        if word not in frequency_words.keys():
            frequency_words[word] = 1
        else:
            frequency_words[word] += 1
    return frequency_words

Now we have the dictionary and it’s time for us to test whether this program works or not. So create a text file with random words, repeat some of them on purpose so that you can check

testfile.png
Continue reading "Challenge TC101 Frequency Dictionary"

Challenge TC101 Frequency Dictionary

--Originally published at Programming

The first challenge posted by ken is the next one:

Using a file full of words, create a program that counts how many times each word is repeated in the file. We are going to do first some functions that’ll help us throughout this process. First we’ll create a function read_from_file which will basically open a file and read from it every word, storing them in a list. This function returns a list of words.

def read_from_file(file):
    """
    :param file: The file words are going to be read from
    :return: a list with the words inside the file
    """
    lista = []
    try:
        f = open(file, 'r')
        for line in f:
            line = line.strip('\n')  # Get rid of the \n at the end of each line
            lista += line.split(" ")  # get every word of the line and store it inside a list
        f.close()
    except IOError:
        print("Cannot open " + file)
    return lista

Now we are going to create a function that counts the frequencies of the words inside a list by storing it in a dictionary. The dictionary keys will be the words and the values will be the number of times each word appears in the list. This function returns a dictionary with frequencies.

def count_frequency(words):
    """
    :param words: List of words
    :return: A dictionary with frequencies of words
    """
    frequency_words = {}
    for word in words:  # iterate over the list
        # If the word is not already inside the dictionary's keys, create it
        if word not in frequency_words.keys():
            frequency_words[word] = 1
        else:
            frequency_words[word] += 1
    return frequency_words

Now we have the dictionary and it’s time for us to test whether this program works or not. So create a text file with random words, repeat some of them on purpose so that you can check

testfile.png
Continue reading "Challenge TC101 Frequency Dictionary"