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.

Advertisements

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

Exercise 6

September 27, 2019

Today’s task is a beginning programmer’s exercise from Bjarne Stroustrup’s book Programming: Principles and Practice Using C++:

Write a program that prompts the user to enter 3 integer values, and then outputs the values in numerical sequence separated by commas. So: if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4 5 4 should give 4, 4, 5.

Your task is to write the indicated program; you may use C++ or any other language, but your solution should be at the level of a beginning programmer who has not yet learned about arrays or user-defined functions. 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

Eureka

September 24, 2019

Somebody’s homework today:

A number is a eureka number if the sum of the powers of its digits, with powers increasing, is equal to the number. For instance, 89 is a eureka number because 8**1 + 9**2 = 89, and 1306 is a eureka number because 1**1 + 3**2 + 6**4 = 1306.

Your task is to write a program to identify eureka numbers, and determine how many eureka numbers exist that are less than a million. 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

What Comes Next?

September 20, 2019

This is a Microsoft interview question:

What is the next number in the sequence: 8, 13, 17, 22, 24, 26, 29, 31, 35, …?

Your task is to solve the puzzle. 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

Getopt

September 17, 2019

As I have said previously on this blog, I’m not allowed to write Scheme code at work; it would never make it past code review. But I can write shell scripts, and Awk is a natural language to use in shell scripts, so lately I have been writing a lot of Awk programs.

I recently had occasion to write an Awk program that took command-line arguments, so naturally I googled “getopt.awk” and found an implementation in the Gnu Awk manual. But that version of getopt was very C-like and not at all Awk-ish, so I decided to write my own. My getopt function extracts command-line arguments into an associative array OPTS that has the flag as its key and the value associated with the flag as its value; the value is null if the flag has no associated value. The getopt function takes two arguments — a string defining the available flags, in the same format as the C getopt function, and a string containing a message to be printed on error — and returns the number of arguments found.

Your task is to write a getopt function for your favorite langauge; if your language already provides command-line argument handling, write a version for Awk. 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

Alchemical Reduction

September 10, 2019

Today’s exercise is from the 2018 Advent of Code, Day 5:

You’ve managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit’s size reduction capabilities.

While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit’s material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).

The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units’ types are represented by letters; units’ polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.

For example:

– In aA, a and A react, leaving nothing behind.

– In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.

– In abAB, no two adjacent units are of the same type, and so nothing happens.

– In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.

Now, consider a larger example, dabAcCaCBAcCcaDA:

dabAcCaCBAcCcaDA  The first 'cC' is removed.
dabAaCBAcCcaDA    This creates 'Aa', which is removed.
dabCBAcCcaDA      Either 'cC' or 'Cc' are removed (the result is the same).
dabCBAcaDA        No further actions can be taken.

After all possible reactions, the resulting polymer contains 10 units.

How many units remain after fully reacting the polymer you scanned?

Your task is to write a program that solves the alchemical reduction. 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

Semi-Primes

September 6, 2019

We have on several occasions seen functions for generating the primes less than n. Sometimes, what you want instead of primes are semi-primes, numbers which are the product of two primes. For instance, the six semi-primes less than 25 are 2×3=6, 2×5=10, 2×7=14, 3×5=15, 3×7=21 and 2×11=22.

Your task is to write a function that makes a list of the semi-primes less than n; use it to discover the number of semi-primes less than 10,000. 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