TopCoder problem "Encoder" used in SRM 24 (Division I Level Two , Division II Level Two)



Problem Statement

    
Class name: Encoder
Method name: encode
Parameters: String, int
Returns: String

Implement a class Encoder, which contains a method encode.  encode takes two
parameters:  a String s and an int c, you are to perform the following
operations on s:

(1) Starting at the 0th character, every 4th character in the string (0th, 4th,
8th, etc) should have its binary ASCII value ANDed with the first (least
significant) 8 bits of the binary representation of c.
(2) Starting at the 1st character, every 4th character in the string (1st, 5th,
9th, etc) should have its binary ASCII value ORed with the second 8 bits of c.
(3) Starting at the 2nd character, every 4th character in the string (2nd, 6th,
10th, etc) should have its binary ASCII value XORed with the third 8 bits of c.
(4) Starting at the 3rd character, every 4th character in the string (3rd, 7th,
11th, etc) should have its binary ASCII value NORed with the last 8 bits of c.
(5) If the result of any operation yields the null character (ASCII value of
0), replace that character with the character 'N' (ASCII value = 78 base 10 =
01001110 base 2).
(6) Finally return the string in reverse order.

Note:
- The "first 8 bits" of c are the 8 least significant bits, "second 8 bits" of
c are the second-to-least significant group of 8, etc... (See The Example)
- In the string the 0th character is the left most character.
- All operations are bit-wise (AND two 8-bit numbers by ANDing each of the 8
bits).
- Use the extended ASCII set (with characters from 0 to 255 base 10)
- The binary ASCII value of a character is the 8 bits base-2 representation of
the ASCII value.  Note this binary value may have leading zeroes.
- If the input is an empty String, output an empty String.
- The output may contain characters with ASCII values anywhere between 1 and
255 - be warned the output may be gibberish.
- If the String is shorter than four characters some of steps (1) - (4) may not
be done.
- The result of ANDing two bits is 1 only if both bits are 1, and is 0 otherwise.
- The result of ORing two bits is 1 only if at least 1 of the two bits is 1,
and is 0 otherwise.
- The result of XORing two bits is 1 only if exactly 1 of the two bits is 1,
and is 0 otherwise.
- The result of NORing two bits is 1 only if both bits are 0, and is 0
otherwise (the opposite of OR).

The method signature is (be sure your method is public):  
String encode(String s,int c);

s can be no longer than 50 characters, and can only contain capital and
lowercase letters, numbers, commas, spaces, and periods.

Examples:

If s = "AAAA" (quotes not included in s) and c = -1694170560, 

A has binary ASCII value 01000001.
-1694170560 has binary value 10011011000001010000001001000000

First, AND the first A with the 8 least significant bits of the integer:
    01000001
AND 01000000
------------
    01000000 -> @

Then, OR the second A with the second group of 8 bits:
    01000001
OR  00000010
------------
    01000011 -> C

Then, XOR the third A with the next 8 bits:
    01000001
XOR 00000101
------------
    01000100 -> D

Then, NOR the final A with the final 8 bits:
    01000001
NOR 10011011
------------
    00100100 -> $

No characters ended up with value 0, so none need to be changed to 'N'.
The returned String should then be the characters generated above, in reverse
order: "$DC@" (quotes are shown here for clarity only).

If s="HI" and c=1024, the method should return the String "MN".  Note the
result of the first AND was 0, so the character was replaced with N. 

If s="This, is a test." and c=123456789, the method return will be displayed as
gibberish but will have 16 characters in the output String with base 10 ASCII
values of, in order, 208, 47, 255, 5, 136, 123, 237, 78, 136, 50, 237, 4, 136,
50, 237, 20.  
 

Definition

    
Class:Encoder
Method:encode
Parameters:String, int
Returns:String
Method signature:String encode(String param0, int param1)
(be sure your method is public)
    

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=145

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=3023&pm=145

Writer:

wluebke

Testers:

Problem categories: