Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:52:34+00:00 2026-06-03T09:52:34+00:00

Say we have a function that translates the morse symbols: . -> -. –

  • 0

Say we have a function that translates the morse symbols:

  • . -> -.
  • - -> ...-

If we apply this function twice, we get e.g:

. -> -. -> ...--.

Given an input string and a number of repetitions, want to know the length of the final string. (Problem 1 from the Flemish Programming Contest VPW, taken from these slides which provide a solution in Haskell).

For the given inputfile

4
. 4
.- 2
-- 2 
--... 50

We expect the solution

44
16
20
34028664377246354505728

Since I don’t know Haskell, this is my recursive solution in Python that I came up with:

def encode(msg, repetition, morse={'.': '-.', '-': '...-'}):
    if isinstance(repetition, str):
        repetition = eval(repetition)
    while repetition > 0:
        newmsg = ''.join(morse[c] for c in msg)
        return encode(newmsg, repetition-1)
    return len(msg)


def problem1(fn):
    with open(fn) as f:
        f.next()
        for line in f:
            print encode(*line.split())

which works for the first three inputs but dies with a memory error for the last input.

How would you rewrite this in a more efficient way?

Edit

Rewrite based on the comments given:

def encode(p, s, repetition):
    while repetition > 0:
        p,s = p + 3*s, p + s
        return encode(p, s, repetition-1)
    return p + s


def problem1(fn):
    with open(fn) as f:
        f.next()
        for line in f:
            msg, repetition = line.split()
            print encode(msg.count('.'), msg.count('-'), int(repetition))

Comments on style and further improvements still welcome

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:52:35+00:00Added an answer on June 3, 2026 at 9:52 am

    Consider that you don’t actually have to output the resulting string, only the length of it. Also consider that the order of ‘.’ and ‘-‘ in the string do not affect the final length (e.g. “.- 3” and “-. 3” produce the same final length).

    Thus, I would give up on storing the entire string and instead store the number of ‘.’ and the number of ‘-‘ as integers.

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

Sidebar

Related Questions

Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
Say i have this function that dynamically creates my namespace for me when I
Say I have a function that returns an array like this... array( 0 =>
Let's say I have a function that looks like this: public void saveBooking(/* some
Say I have a function that takes an arbitrary number of arguments (the last
Say I have a function foo that I want to call n times. In
Lets say I have a recursive function that creates lists within lists. It return
Say I have a function func(i) that creates an object for an integer i,
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Many people have argued about function size. They say that functions in general should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.