Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 13281
Next
In Process

askthedev.com Latest Questions

Asked: September 26, 20242024-09-26T21:51:43+05:30 2024-09-26T21:51:43+05:30In: Python

“How can I decode Morse code into text using the fewest characters in Python?”

anonymous user

I’ve been diving deep into the world of Morse code lately and stumbled across an interesting challenge that I thought would be fun to share! So, here’s the scenario: you get a string of Morse code, which consists of dots and dashes, and you need to decode it into plain text.

Now, the catch is that I want you to do this in the fewest characters of code possible. I’ve seen a variety of ways to tackle this, but I’m looking for the most succinct and clever method you can come up with!

The Morse code representation is simple: each letter corresponds to a unique combination of dots (.) and dashes (-). For example, A is “.-“, B is “-…”, etc. You can find the full chart online or in any Morse code reference.

To make things a bit more interesting, let’s add a twist. You have to consider spaces between letters and between words. A single space (‘ ‘) should separate letters, while a slash (‘/’) should be used to denote the gap between words. So if we were to decode “.- / -…” that should translate to “A B”.

Here’s an example of what the input and output could look like:

Input:
“`
…. . .-.. .-.. — / .– — .-. .-.. -..
“`

Expected output:
“`
HELLO WORLD
“`

The real challenge lies in keeping your code as compact as possible. It’s easy to overthink it and end up writing something lengthy, but I’ve seen some incredibly compact solutions that pack a punch.

I’d love to see how you all approach this and what creative code snippets you come up with. Do you have any shortcuts or specific techniques that you think would help minimize character count? I know some of you are masters at this kind of thing, so feel free to share your thought processes and solutions. Let’s get decoding!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2024-09-26T21:51:45+05:30Added an answer on September 26, 2024 at 9:51 pm

      morse={'.-':'A','-...':'B','-..':'D','-..-.':'/','..-.':'F','--.':'G','....':'H','..':'I','.':'E','.---':'J','-.-':'K','.-..':'L','--':'M','-.':'N','---':'O','.--.':'P','--.-':'Q','.-.':'R','...':'S','-':'T','..-':'U','...-':'V','.--':'W','-..-':'X','-.--':'Y','--..':'Z'}; 
      decode=m=>m.split(' ').map(w=>w=='/'?' ':morse[w]).join('');
      console.log(decode('.... . .-.. .-.. --- / .-- --- .-. .-.. -..'));
      

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2024-09-26T21:51:44+05:30Added an answer on September 26, 2024 at 9:51 pm

      morse={".-":"A","-...":"B","-..":"D","-..":"D","." :"E","..-.":"F","--.":"G","....":"H","..":"I",".---":"J","-.-":"K",".-..":"L","--":"M","-.":"N","---":"O",".--.":"P","--.-":"Q",".-.":"R","...":"S","-":"T","..-":"U","...-":"V",".--":"W","-..-":"X","-.--":"Y","--..":"Z"," ":" "}
      decode=c=>c.split` / `.map(w=>w.split` `.map(l=>morse[l]).join``).join` `
      console.log(decode(`.... . .-.. .-.. --- / .-- --- .-. .-.. -..`));
      

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Related Questions

    • What is a Full Stack Python Programming Course?
    • How to Create a Function for Symbolic Differentiation of Polynomial Expressions in Python?
    • How can I build a concise integer operation calculator in Python without using eval()?
    • How to Convert a Number to Binary ASCII Representation in Python?
    • How to Print the Greek Alphabet with Custom Separators in Python?

    Sidebar

    Related Questions

    • What is a Full Stack Python Programming Course?

    • How to Create a Function for Symbolic Differentiation of Polynomial Expressions in Python?

    • How can I build a concise integer operation calculator in Python without using eval()?

    • How to Convert a Number to Binary ASCII Representation in Python?

    • How to Print the Greek Alphabet with Custom Separators in Python?

    • How to Create an Interactive 3D Gaussian Distribution Plot with Adjustable Parameters in Python?

    • How can we efficiently convert Unicode escape sequences to characters in Python while handling edge cases?

    • How can I efficiently index unique dance moves from the Cha Cha Slide lyrics in Python?

    • How can you analyze chemical formulas in Python to count individual atom quantities?

    • How can I efficiently reverse a sub-list and sum the modified list in Python?

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.