loading...

Daily Challenge #100 - Round Up

thepracticaldev profile image dev.to staff ・2 min read

We started our daily challenge series on June 28th and it has been an amazing experience seeing your comments, suggestions, and solutions these past few months. In this post, I've collected some of our most popular challenges. If you have any of these challenges in your reading list, maybe try your hand at them!


Our first challenge post asked users to simply remove the first and last characters off a string. You all gave a variety of answers in CSS, Ruby, and Javascript!

@alvaromontoro got the most popular comment on this post with 43 hearts. He's been a great help to many around our community, you'll see his comments on a lot of our challenge posts.

CSS

Just add the class removeFirstAndLastLetter to a tag, and see the first and last letter "removed" from the text 😋

.removeFirstAndLastLetter {
  background: #fff;
  font-family: monospace;
  white-space: nowrap;
  position: relative;
  display: inline-block;
}

.removeFirstAndLastLetter::before,
.removeFirstAndLastLetter::after {
  content: "\00a0";
  background: #fff;
  top: 0;
  display: block;
  position: absolute;
  height: 100%;
  width: auto;
}

.removeFirstAndLastLetter::after {
  right: 0;
}

And as an extra, it can be stylized and animated, so you can see the letters fade out:



We really thought this post would be trickier, but a lot of really short, thoughtful answers came in. Daily Challenge #48 asked users to write a function that mimics the syntax common on Facebook.

@alvaromontoro got the most popular comment again, even 47 posts later! @avalander also gave a great answer using Haskell and got 15 hearts:

Some simple pattern matching with Haskell.

likes :: [String] -> String
likes []        = "no one likes this"
likes [a]       = a ++ " likes this"
likes [a, b]    = a ++ " and " ++ b ++ " like this"
likes [a, b, c] = a ++ ", " ++ b ++ " and " ++ c ++ " like this"
likes (a:b:xs)  = a ++ ", " ++ b ++ " and " ++ (show $ length xs) ++ " others like this"


Daily Challenge #31 prompted users to return the number of IP addresses between a starting point and an ending point.

@phallstrom found a cheeky solution by using Ruby's IPAddr class. @kerrishotts went above and beyond and made the function work if the points were reversed, great work!

Here's mine! I extended it a little to work if the start and end were reversed.

const convertIPStringToNumber = ipString => ipString.split(".")
    .reduce((acc, part) => (acc << 8) | Number(part), 0);

const ipsBetween = (start, finish) => 
    Math.abs(convertIPStringToNumber(finish) - convertIPStringToNumber(start));

Gist: gist.github.com/kerrishotts/797450...



Our 60th challenge asked users to write a function that takes an array of consecutive letters as input and that returns the missing letter in the array.

@citizen428 offered some improvements on a Go answer left by @dak425 , and even left his own answer in F#!

Time to Go find the missing letter!

missing_letter.go

package find

// MissingLetter indicates what the missing character is in a ordered sequence of characters
func MissingLetter(chars []rune) rune {
    var last int

    for _, r := range chars {
        if last != 0 && int(r)-last > 1 {
            return rune(r - 1)
        }
        last = int(r)
    }

    return rune(last)
}

missing_letter_test.go

package find

import "testing"

type testCase struct {
    description string
    input       []rune
    expected    rune
}

func TestMissingLetter(t *testing.T) {
    testCases := []testCase{
        {
            "two characters",
            []rune{'A', 'C'},
            'B',
        },
        {
            "dev-to example one",
            []rune{'a', 'b', 'c', 'd', 'f'},
            'e',
        },
        {
            "dev-to example two",
            []rune{'O', 'Q', 'R', 'S'},
            'P',
        },
    }

    for _, test := range testCases {
        if result := MissingLetter(test.input); result != test.expected {
            t.Fatalf("FAIL: %s - MissingLetter(%+v): %v - expected %v", test.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.description)
    }
}



The Caesar Cipher is a well-known challenge for beginner programmers to start thinking about larger concepts like data encryption. Daily Challenge #42 asked users to work with the cipher, gave a shift key and a string and asked for the plain text.

@ynndvn used the brute force method on this challenge post back in August and I have seen them commenting their solutions on our latest challenges. Awesome work!

Here we go:

f=(s)=>[...Array(26)].map((_,i)=>26-i).map(n=>`key: ${26-n}\ncipher: ${s}\nplain: ${[...s].map(l=>(a=l.charCodeAt(0),String.fromCharCode(a<123&&a>96?97+((a-97+n)%26):a))).join('')}`).join`\n---\n`

Without any proper dictionary, I guess I'll stick to the "bruteforce" method! Here's how it works:

  • First, build an Array containing numbers from 26 to 1 ([...Array(26)].map((_,i)=>26-i))
  • Print the current index (26-n) and the cipher (s)
  • Build the associated original text: For each character, if its charCode is between 97 and 122 (hence, if it is a valid lowercase character), add the current index to it, else just return it.
  • Finally, print the complete result
f('dwwdfn iurp wkh zrrgv dw gdzq');

"key: 0
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: dwwdfn iurp wkh zrrgv dw gdzq
---
key: 1
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: cvvcem htqo vjg yqqfu cv fcyp
---
key: 2
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: buubdl gspn uif xppet bu ebxo
---
key: 3
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: attack from the woods at dawn
---
key: 4
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: zsszbj eqnl sgd vnncr zs czvm
---
key: 5
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: yrryai dpmk rfc ummbq yr byul
---
key: 6
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: xqqxzh colj qeb tllap xq axtk
---
key: 7
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: wppwyg bnki pda skkzo wp zwsj
---
key: 8
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: voovxf amjh ocz rjjyn vo yvri
---
key: 9
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: unnuwe zlig nby qiixm un xuqh
---
key: 10
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: tmmtvd ykhf max phhwl tm wtpg
---
key: 11
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: sllsuc xjge lzw oggvk sl vsof
---
key: 12
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: rkkrtb wifd kyv nffuj rk urne
---
key: 13
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: qjjqsa vhec jxu meeti qj tqmd
---
key: 14
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: piiprz ugdb iwt lddsh pi splc
---
key: 15
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: ohhoqy tfca hvs kccrg oh rokb
---
key: 16
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: nggnpx sebz gur jbbqf ng qnja
---
key: 17
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: mffmow rday ftq iaape mf pmiz
---
key: 18
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: leelnv qczx esp hzzod le olhy
---
key: 19
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: kddkmu pbyw dro gyync kd nkgx
---
key: 20
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: jccjlt oaxv cqn fxxmb jc mjfw
---
key: 21
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: ibbiks nzwu bpm ewwla ib liev
---
key: 22
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: haahjr myvt aol dvvkz ha khdu
---
key: 23
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: gzzgiq lxus znk cuujy gz jgct
---
key: 24
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: fyyfhp kwtr ymj bttix fy ifbs
---
key: 25
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: exxego jvsq xli asshw ex hear"

Thank you to CodeWars for allowing redistribution under the 2-Clause BSD License.

Special thanks to @peledzohar and @aminnairi for contributing to the series. Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion

pic
Editor guide
Collapse
antogarand profile image
Antony Garand

And here I was, thinking we had to actually round up a number!

Here is my answer anyway, because I find it interesting.

JS answer:

const roundUp = x=>~~++x
Collapse
peter profile image
Peter Kim Frank

A special shout out to @devencourt , @averyd , and @aikedaa who have all done a great job behind the scenes in setting up these community challenges!