4
\$\begingroup\$

Your task is to hide a small message or string inside a larger block of text. To do so, you must hide the message as capital letters inside the larger

For the quoted test:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut convallis porta varius. Donec loBortis adipiscing dolor ac Euismod. Ut Mincidunt eY cursus nunc pulVinar, eu mAttis nibh tincidunt. PhaseLlus sEd molestie sapieN. InTeger congue maurIs in Nisi faucibus, at aliquam ligula consEctetur. Quisque laoreet turpis at est cursus, in consequat.

(Emboldened text only to highlight where the letters are)

The secret text reads:

BE MY VALENTINE

You choose how to spread the words out, and should error if you try to hide a string inside another that won't fit either from length or lack of required characters.

Assume the input is plain text, where the first letter of each sentence may be capitalised or not (those can be lower cased or ignored, but should not be part of the secret message, for clarity).

You'll get -10 points for writing a second program that recovers the hidden message from a block of text. In this eventuality your score is the average of the two, minus the 10 bonus points.

E.g. If the Message hiding code is 73 characters, and the recovery one is 80, your score is:

(73+80)/2-10=66.5
asked Jan 30, 2014 at 22:36
\$\endgroup\$
6
  • \$\begingroup\$ Given the order of operations, I think you mean: (73+80)/2-10=66.5 \$\endgroup\$ Commented Jan 30, 2014 at 22:48
  • \$\begingroup\$ @user2509848 Yes I did. \$\endgroup\$ Commented Jan 30, 2014 at 22:50
  • \$\begingroup\$ What format do we take the input? And can we assume the block of text will not have capital letters apart from the ones beginning each sentence? \$\endgroup\$ Commented Jan 30, 2014 at 22:59
  • \$\begingroup\$ @Chron Yes to your assumption. \$\endgroup\$ Commented Jan 30, 2014 at 23:02
  • \$\begingroup\$ Can we assume that the first letter of each sentence of input is already capitalized? \$\endgroup\$ Commented Jan 30, 2014 at 23:07

2 Answers 2

3
\$\begingroup\$

Ruby, (69+35)/2-10 = 42

a=gets
$><<gets.gsub(/./){a[0]!=$&?$&:a.slice!(0).upcase}
1/0if a>?\n

Takes input on STDIN, first line is the message to hide, second line is the text block. Assumes the secret message is lower-case. Example input:

codegolf
You'll get -10 points for writing a second program that recovers the hidden message from a block of text. In this eventuality your score is the average of the two, minus the 10 bonus points.

Example output:

You'll get -10 points for writing a seCOnD program that rEcovers the hidden messaGe frOm a bLock oF text. In this eventuality your score is the average of the two, minus the 10 bonus points.

Here's the program to obtain the secret back from the output (again, provided on STDIN):

$><<gets.scan(/(?<!^|\. )[A-Z]/)*''

One caveat here is that Ruby's lookbehind expressions have to be fixed width, so this code only works for text where the periods are followed by exactly one space. Suggestions welcome!

answered Jan 30, 2014 at 23:13
\$\endgroup\$
2
\$\begingroup\$

Python 3, (136 + 74) / 2 - 10 = 95

Encoder (message must be lowercase):

t,m=input(),list(input().replace(' ', ''))
print([''.join(map(lambda c:m.pop(0).upper()if c.islower()and c in m[:1]else c,t))][bool(m)])

Decoder:

import re
print(filter(str.isupper, re.sub(r'(^|\. )[A-Z]', '', input())))
answered Feb 1, 2014 at 23:35
\$\endgroup\$
2
  • \$\begingroup\$ -6 bytes in Encoder: if c in m[:c.islower()]else c \$\endgroup\$ Commented Mar 5, 2024 at 7:27
  • \$\begingroup\$ Strip the three extra whitespaces in the Decoder for -3 bytes. \$\endgroup\$ Commented Mar 5, 2024 at 7:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.