Skip to content

Instantly share code, notes, and snippets.

@Haumer
Created July 10, 2020 20:12
Show Gist options
  • Select an option

  • Save Haumer/6c4d8fa9753ca599eb96b59d5805849d to your computer and use it in GitHub Desktop.

Select an option

Save Haumer/6c4d8fa9753ca599eb96b59d5805849d to your computer and use it in GitHub Desktop.
Computer science is the study of the theory, experimentation, and engineering that form the basis for the design and use of computers. It is the scientific and practical approach to computation and its applications and the systematic study of the feasibility, structure, expression, and mechanization of the methodical procedures (or algorithms) that underlie the acquisition, representation, processing, storage, communication of, and access to, information. An alternate, more succinct definition of computer science is the study of automating algorithmic processes that scale. A computer scientist specializes in the theory of computation and the design of computational systems.[1]
Its fields can be divided into a variety of theoretical and practical disciplines. Some fields, such as computational complexity theory (which explores the fundamental properties of computational and intractable problems), are highly abstract, while fields such as computer graphics emphasize real-world visual applications. Other fields still focus on challenges in implementing computation. For example, programming language theory considers various approaches to the description of computation, while the study of computer programming itself investigates various aspects of the use of programming language and complex systems. Human–computer interaction considers the challenges in making computers and computations useful, usable, and universally accessible to humans.
# create a method 'frequencies' that takes one string as an argument => 'text'
def frequencies(text)
# 1.) split text into an array of words
words = text.split
# 2.) define an empty hash.
the_frequencies = Hash.new(0)
# the_frequencies = {}
# 3.) iterate of the words array
words.each do |word|
# 4.) create a key value pair where the word is the key and the value is the frequency
the_frequencies[word] += 1
end
# 5.) return the hash
the_frequencies
end
# Separate method to read the text from the 'article.txt' file
def read_file(path)
File.open(path).read.strip
end
# example:
text = read_file('article.txt')
p frequencies(text)
require_relative "../frequencies"
describe "#frequencies" do
it "returns an empty Hash when passed an empty string" do
the_frequencies = frequencies("")
expect(the_frequencies).to eq({})
end
it "counts multiple words" do
the_frequencies = frequencies("the lazy dog jumped over the brown fox")
expect(the_frequencies["the"]).to eq(2)
expect(the_frequencies["dog"]).to eq(1)
expect(the_frequencies["cat"]).to eq(0)
end
end
require_relative "frequencies"
article = read_file("article.txt")
words = frequencies(article)
# here we sort the words in the hash by their frequency
words.sort_by { |word, frequency| frequency }.reverse.each do |word, frequency|
puts "#{frequency.to_s.rjust(2)}: #{word}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment