Sorry for the terrible title, that is literally what the problem is.
I'm doing an exercise in a book I'm learning which wants me to:
From a list of a lot of words, find only the words that start with "D", uppercase the words, and sort them by smallest to biggest.
This is what I have so far:
defmodule ReadFile do
def findD(contents) do
contents
|> String.split("\n")
|> Enum.filter(fn(word) -> String.starts_with?(word, "D") end)
|> Enum.map(fn(word) -> String.upcase(word) end)
|> Enum.sort(fn(el1, el2) -> String.length(el1) > String.length(el2) end)
end
end
The data I'm passing to findD
is
(Just a string of a bunch of words read from a file.txt`
"adult\n\naeroplane\n\nair\n\naircraft Carrier\n\nairforce\n\nairport\n\nalbum\n\nalphabet\n\napple\n\narm\n\narmy\n\nbaby\n\nbaby\n\nbackpack\n\nballoon\n\nbanana\n\nbank\n\nbarbecue\n\nbathroom\n\nbathtub\n\nbed\n\nbed\n\nbee\n\nbible\n\nbible\n\nbird\n\nbomb\n\nbook\n\nboss\n\nbottle\n\nbowl\n\nbox\n\nboy\n\nbrain\n\nbridge\n\nbutterfly\n\nbutton\n\ncappuccino\n\ncar\n\ncar-race\n\ncarpet\n\ncarrot\n\ncave\n\nchair\n\nchess Board\n\nchief\n\nchild\n\nchisel\n\nchocolates\n\nchurch\n\nchurch\n\ncircle\n\ncircus\n\ncircus\n\nclock\n\nclown\n\ncoffee\n\ncoffee-shop\n\ncomet\n\ncompact Disc\n\ncompass\n\ncomputer\n\ncrystal\n\ncup\n\ncycle\n\ndata Base\n\ndesk\n\ndiamond\n\ndress\n\ndrill\n\ndrink\n\ndrum\n\ndung\n\nears\n\nearth\n\negg\n\nelectricity\n\nelephant\n\neraser\n\nexplosive\n\neyes\n\nfamily\n\nfan\n\nfeather\n\nfestival\n\nfilm\n\nfinger\n\nfire\n\nfloodlight\n\nflower\n\nfoot\n\nfork\n\nfreeway\n\nfruit\n\nfungus\n\ngame\n\ngarden\n\ngas\n\ngate\n\ngemstone\n\ngirl\n\ngloves\n\ngod\n\ngrapes\n\nguitar\n\nhammer\n\nhat\n\nhieroglyph\n\nhighway\n\nhoroscope\n\nhorse\n\nhose\n\nice\n\nice-cream\n\ninsect\n\njet fighter\n\njunk\n\nkaleidoscope\n\nkitchen\n\nknife\n\nleather jacket\n\nleg\n\nlibrary\n\nliquid\n\nmagnet\n\nman\n\nmap\n\nmaze\n\nmeat\n\nmeteor\n\nmicroscope\n\nmilk\n\nmilkshake\n\nmist\n\nmoney $$$$\n\nmonster\n\nmosquito\n\nmouth\n\nnail\n\nnavy\n\nnecklace\n\nneedle\n\nonion\n\npaintBrush\n\npants\n\nparachute\n\npassport\n\npebble\n\npendulum\n\npepper\n\nperfume\n\npillow\n\nplane\n\nplanet\n\npocket\n\npost-office\n\npotato\n\nprinter\n\nprison\n\npyramid\n\nradar\n\nrainbow\n\nrecord\n\nrestaurant\n\nrifle\n\nring\n\nrobot\n\nrock\n\nrocket\n\nroof\n\nroom\n\nrope\n\nsaddle\n\nsalt\n\nsandpaper\n\nsandwich\n\nsatellite\n\nschool\n\nsex\n\nship\n\nshoes\n\nshop\n\nshower\n\nsignature\n\nskeleton\n\nslave\n\nsnail\n\nsoftware\n\nsolid\n\nspace Shuttle\n\nspectrum\n\nsphere\n\nspice\n\nspiral\n\nspoon\n\nsports-car\n\nspot Light\n\nsquare\n\nstaircase\n\nstar\n\nstomach\n\nsun\n\nsunglasses\n\nsurveyor\n\nswimming Pool\n\nsword\n\ntable\n\ntapestry\n\nteeth\n\ntelescope\n\ntelevision\n\ntennis racquet\n\nthermometer\n\ntiger\n\ntoilet\n\ntongue\n\ntorch\n\ntorpedo\n\ntrain\n\ntreadmill\n\ntriangle\n\ntunnel\n\ntypewriter\n\numbrella\n\nvacuum\n\nvampire\n\nvideotape\n\nvulture\n\nwater\n\nweapon\n\nweb\n\nwheelchair\n\nwindow\n\nwoman\n\nworm\n\nx-ray\n\n"
In iex
I'm doing
{ :ok, fileContents } = File.read("sample.txt")
ReadFile.findD(fileContents)
Which returns []
I'm unsure on why, I also don't know how to debug a pipeline
I tried inserting
|> IO.puts
after each call in the pipe line but that just resulted in errors. Any help would be great thanks.
If the word list you have provided is the one you are actually using then the problem is that there are no words that start with "D"
so []
is the correct result.
If you want words that start with a lower case "d"
then you will have to replace this line:
Enum.filter(fn(word) -> String.starts_with?(word, "D") end)
With:
Enum.filter(fn(word) -> String.starts_with?(word, "d") end)
If you want to check for both you can do:
Enum.filter(fn(word) -> String.at(word, 0) in ["d", "D"] end)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments