Crypt(1)

October 29, 2019

I was surprised to learn recently that modern Linux systems do not provide a crypt command, which was common in older Unix systems. So I decided to write one.

The interface to crypt is simple: it reads from standard input and writes to standard output, requesting a key from the console using a method that doesn’t echo the key to the screen. The program has no command-line arguments.

The cipher is symmetric, so encryption and decryption use the same key; the ciphering algorithm I choose is <a href="“>RC4-drop512, which isn’t exactly state-of-the-art encryption, but is simple and good enough for casual use. The base algorithm is RC4; dropping the first 512 characters of the generated keystream eliminates a potential weakness in the bas RC4 key scheduling algorithm.

Your task is to write a crypt program as described above. When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

IBAN

October 25, 2019

The International Bank Account Number (IBAN) is an internationally agreed standard of identifying bank accounts with a hash number to reduce errors. The first two characters of an IBAN are the two-letter country code, the next two characters are a two-digit hash, and the remaining characters identify the bank routing number and depositor account number. For instance, here is a fictition British IBAN: GB82 WEST 1234 5698 7654 32. The full IBAN standard specifies the range of valid bank account numbers and depositor account numbers for each country; we are interested only in the hash code.

In the code shown above, the hash code consists of the two digits 82, which are validated as follows:

1) Move the first four characters from the beginning of the string to the end: WEST 1234 5698 7654 32GB 82.

2) Replace letters with two-digit numbers according to the scheme A = 10, …, Z = 35:
3214 2829 1234 5698 7654 3216 1182.

3) Treating the string as a large integer, divide by 97 and calculate the remainder.

4) If the remainder is not 1, the IBAN is not valid.

To generate a hash code, perform the same procedure as above with the two hash digits initially set to 00, then subtract the hash code from 98 and insert it in the IBAN.

Your task is to write functions to validate existing IBAN numbers and generate hash codes for new IBAN numbers. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

A Silly Task

October 22, 2019

Today’s task is silly:

Your task is to write a program that, when executed, writes to the output in words the number of characters in the program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Anagrams, Again

October 18, 2019

The previous exercise showed a probabilistic method for determining if two strings are anagrams, and some users pointed out collisions in the method that falsely concluded two strings were anagrams when in fact they were not.

Your task is to write a program that recognizes anagrams without possibility of error. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Prime Anagrams

October 15, 2019

There are many algorithms for recognizing anagrams. Here is an unusual one:

Assign each character to a prime number, then map a string to the corresponding primes, and compute their product. Two strings are anagrams only if they share a product.

Your task is to use prime numbers to recognize anagrams. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

A Scrambled Words Variant

October 11, 2019

In the previous exercise, we wrote a program to scramble the letters in each word of a string, retaining capitalization and punctuation: “Programming Praxis is fun!” became something like “Grprnimaogm Srxpia is unf!” Today’s exercise is the same, but with a simple variation: the first and last letters of each word must remain unchanged, so we might see something like “Prgrnimaomg Prxias is fun!”

Your task is to modify your previous program in the minimal possible way into a program that scrambles the letters in the interior of the words in a string, retaining capitalization and punctuation. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Scrambled Words

October 8, 2019

Given a string, scramble the letters of each word individually, where a word is a maximal sequence of letters, maintaining the original capitalization and punctuation. For instance, the string “Programming Praxis is fun!” might be scrambled as “Grprnimaogm Srxpia is unf!”

Your task is to write a program to scramble the letters of each word in a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Neatly Printing A CSV File

October 4, 2019

Today’s exercise can be either simple or complex, depending on how you define it.

The task is to write a program to neatly print a CSV file. Define “neatly” however you wish. The CSV format has inherently variable-width columns, but you should print the columns neatly aligned. Pages should have headers and footers. Add other features as you wish.

Your task is to write a program to neatly print a CSV file. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2 3

Intersecting Number Wheels

October 1, 2019

Today’s exercise is a new task from Rosetta Code:

A number wheel has a name which is an uppercase letter and a set of ordered values which are either numbers or names. A number is generated from a named wheel by starting at the first value of the names wheel and advancing through subesquent values then wrapping around to the first value to form a “wheel”, according to the following rules: If the value is a number, yield it; if the value is a name, yield the next value from the named wheel; advance the position of the wheel. For instance, given two wheels A: 1 B 2 and B: 3 4, the sequence generated for wheel A is 1 3 2 1 4 2 1 3 2 1 4 2 ….

Your task is to write a program to calculate the sequences generated by intersecting number wheels. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2