DEV Community

dev.to staff
dev.to staff

Posted on

7

Daily Challenge #182 - Arrh, grabscrab!

Setup

Pirates are notoriously bad at pronunciation. For this challenge, implement a function that will accept a jumbled word and a dictionary. It should output the words that the pirate might have been saying.

Example

grabscrab( "ortsp", ["sport","parrot","ports","matey"])
=> ["sport", "ports"]

Tests

grabscrab("trisf", ["first"])
grabscrab("oob", ["bob", "baobab"])
grabscrab("ainstuomn", ["mountains", "hills", "mesa"])
grabscrab("oolp", ["donkey", "pool", "horse", "loop"])

Good luck!


This challenge comes from matstc on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (8)

Collapse
 
aminnairi profile image
Amin • Edited

Elm

toSortedList : String -> List Char
toSortedList string =
    List.sort <| String.toList string


equal : String -> String -> Bool
equal first second =
    toSortedList first == toSortedList second


grabscrab : String -> List String -> List String
grabscrab string list =
    List.filter (equal string) list
Collapse
 
fitsum profile image
fitsum • Edited

JavaScript

const grabscrab = (garble, dict) => {
  let results = [];
  dict.forEach(entry => {
    let points = 0; 
    entry.split('').forEach(letter => {
      if (garble.indexOf(letter) !== -1) {
        points++;
        if (points === entry.length) {
          results.push(entry);
          console.log(results)
        }
      }
    })
  })
  return results;
}
Collapse
 
bhaumin profile image
Bhaumin Shah

JavaScript solution:

function grabscrab(word, dict) {
  const wordSorted = word.split("").sort().join("");
  const ans = [];

  for (let item of dict) {
    itemSorted = item.split("").sort().join("");
    if (itemSorted === wordSorted) {
      ans.push(item);
    }
  }

  return ans;
}
  1. Sort the input word just once outside the loop
  2. Just compare the full sorted words instead of each char to avoid false positives in comparing words like aabbbcc to aaabccc
Collapse
 
toanleviettiki profile image
Toan Le Viet

Quick solution in my head with JS is sorting then comparing with each anagram:

Collapse
 
cipharius profile image
Valts Liepiņš

Ruby

def grabscrab word, dict
    orig = word.chars.sort!
    dict.filter {|entry| entry.chars.sort.eql? orig }
end
Collapse
 
cipharius profile image
Valts Liepiņš

Also was in mood for some Haskell.
This time Haskell solution came out neater than Ruby's

import Data.List (sort)

grabscrab :: String -> [String] -> [String]
grabscrab word = filter $ (patt ==) . sort
    where patt = sort word
Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

What is the expected output of the tests?

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay