Programming exercise: permutations of a string
[
Permalink]
Published by
davinci on
October 8, 2009 in
programming and technical issues.
Tags: C++, characters, chars, comparison of programming languages, map, permutations, programming, programming exercises, Python, recursion, Scheme, strings.
The problem is to implement a function that outputs all possible permutations of the characters in a string. Unlike combinations, two permutations are considered distinct if they contain the same characters, but in a different order. Also, for the purposes of this exercise, each occurrence of a repeated character is considered to be a distinct character. That is, if the input is “aaa”, the output should be six repetitions of “aaa”. The permutations may be output in any order.
This exercise, like the previous one on combinations of a string, is from the book Programming Interviews Exposed
by John Mongan and Noah Suojanen\(\)… » [Expand post]« [Collapse post] [Permalink]
Programming exercise: combinations of a string
[
Permalink]
Published by
davinci on
October 7, 2009 in
programming and technical issues.
Tags: C++, characters, chars, combinations, comparison of programming languages, map, permutations, power set, programming, programming exercises, Python, recursion, Scheme, strings.
The problem is to implement a function that outputs all possible combinations of the characters in a string (with length ranging from one to the length of the string). Unlike permutations, two combinations are considered to be the same if they contain the same characters, but in a different order. Another way to define the problem is to find the power set of the characters of the string (excluding the empty set).
Like the previous exercise, this one is also from the book Programming Interviews Exposed
by John Mongan and Noah Suojanen\(\)… » [Expand post]« [Collapse post] [Permalink]
Programming exercise: maximum value in integer array, part 1
[
Permalink]
Published by
davinci on
September 30, 2009 in
programming and technical issues.
Tags: arrays, C++, Common Lisp. Scheme, comparison of programming languages, fold, int, integers, iteration, iterators, Java, Lisp, max, maximum, pointers, programming, programming exercises, Python, recursion, reduce, STL, tail recursion.
This exercise is just a little bit more substantial than the last one — but not by very much. Given an array of \(n\) non-negative integers, find the maximum value in the array, or return \(-1\) if the array is empty. Obviously, the use of any built-in maximum-finding function is forbidden. While the problem is almost trivial, it does illustrate how each language works with array or vector data types, as well as how it handles iteration.
This exercise, like some of the other ones that I will also be going through, is from the book Programming Interviews Exposed
by John Mongan and Noah Suojanen… » [Expand post]« [Collapse post] [Permalink]