Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma.
functionformatWords(words){if(!words||words.length<1)return'';words=words.filter(val=>!!val);constlength=words.length;if(!length)return'';returnwords.reduce((acc,cumul,index)=>{returnindex+1===length?`${acc} and ${cumul}`:`${acc}, ${cumul}`});}
Hello! My name is Thomas and I'm a nerd. I like tech and gadgets and speculative fiction, and playing around with programming. It's not my day job, but I'm working on making it a side gig :)
Nim supports specifying ranges both in absolute terms and in relative terms. ^2 would be the second to last position of a range, etc :) As you can see at the end of the procedure you can also supply just one end of the range and Nim will infer the other end, so filtered[^1] would be just the final element of the sequence.
importsequtils# for filterproc format_words(words:seq[string]):string=result=""ifwords.len>0:varfiltered=filter(words,proc(w:string):bool=w.len>0)forwordinfiltered[0..^2]:result.add(word&", ")returnresult[0..^3]&(" and "&filtered[^1])echoformat_words(@["ninja","samurai","ronin","leonardo","michelangelo","donatello","raphael"])echoformat_words(@["ninja","samurai","ronin"])echoformat_words(@["ninja","","ronin"])echoformat_words(@[])
defformat_words(words):sentence=''ifwords:words=[wordforwordinwordsifword]forindex,wordinenumerate(words):ifindex==0:sentence+=wordelifindex==len(words)-1:sentence+=" and "+wordelse:sentence+=", "+wordreturnsentence
More humane version (kinda like a string builder but using a stream):
(defunformat-words(words)(with-output-to-string(s)(formats"~{~a~^, ~}"(butlastwords))(unless(=1(lengthwords))(formats" and "))(formats"~a"(car(lastwords)))))
functionformatWords(words){if(!words)return'';words=words.filter((word)=>word.length);letreturnString='';for(leti=0;i<words.length;i++){if(i===words.length-1&&i>0){returnString+=` and `;}elseif(i>0){returnString+=`, `;}returnString+=words[i];}returnreturnString;}
#! /usr/bin/perlusewarnings;usestrict;sub format_words{my@words=greplength,@_;my$last=pop@words;returnjoin(', ',@words).(@words?" and $last":"")}useTest::Moretests=>3;isformat_words('ninja','samurai','ronin'),'ninja, samurai and ronin';isformat_words('ninja','','ronin'),'ninja and ronin';isformat_words(),"";
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Wednesday - Format words into a sentence (6 KYU)
Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma.
codewars.com/kata/format-words-int...
My Js solution:
Ruby
My Nim solution :)
Nim supports specifying ranges both in absolute terms and in relative terms. ^2 would be the second to last position of a range, etc :) As you can see at the end of the procedure you can also supply just one end of the range and Nim will infer the other end, so filtered[^1] would be just the final element of the sequence.
Rust Solution:
This is a fun way of doing it, though it might be a bit confusing at first glance 😂
Common Lisp
Inhumane
format
version:appropriated from Practical Common Lisp
More humane version (kinda like a string builder but using a stream):
Perl solution, tests included: