Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Animal foods drill: https://programmingpraxis.com/2021/01/12/animal-txt
- # Requirement: a text file containing information about the dietary patterns of various animals
- function ParseDataFile(fn::String = "animal.txt")
- f = open(fn, "r")
- lines = readlines(f)
- close(f)
- return lines
- end
- function OutputResults(D::Array{String, 2})
- animals = D[:,1]
- foods = D[:,2]
- for food in unique(foods)
- animals_for_given_food = unique(D[D[:,2] .== food ,1])
- c = length(animals_for_given_food)
- println("Food: ", uppercasefirst(food), " Animals who ate it: ", c)
- println("=======")
- for i = 1:c; println(animals_for_given_food[i]); end
- println("")
- end
- end
- function main(fn::String = "animal.txt")
- lines = ParseDataFile(fn)
- n = length(lines)
- animal = ""
- D = Array{String}(undef, n, 2)
- AnimalFound = false
- FoundFood = false
- c = 0
- for line in lines
- if !isempty(line)
- temp = split(line, " ")
- if AnimalFound
- NewCombo = true
- for i = 1:c
- if D[i,1] == animal && D[i,2] == temp[end]; NewCombo = false; end
- end
- if NewCombo
- c += 1
- D[c,:] .= animal, temp[end]
- FoundFood = true
- end
- else
- animal = temp[end]
- AnimalFound = true
- end
- else
- if FoundFood
- AnimalFound = false
- FoundFood = false
- end
- end
- end
- OutputResults(D[1:c,:])
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement