## Three Homework Problems

### September 18, 2015

The *n* th odd number is 2*n*−1, so the recursive function counts down from *n* to zero:

`(define (sum-odd n)`

(if (zero? n) 0

(+ (sum-odd (- n 1)) n n -1)))

```
```

`> (sum-odd 5)`

25

That’s not tail recursive, so the stack will blow out if *n* is too large. Here’s a tail-recursive version of the function:

`(define (sum-odd-aux n sum)`

(if (zero? n) sum

(sum-odd-aux (- n 1) (+ sum n n -1))))

```
```(define (sum-odd n)

(sum-odd-aux n 0))

`> (sum-odd 5)`

25

If you’re cheeky, you might use a mathematical identity to simplify your solution; no recursion required:

`(define (sum-odd n) (* n n))`

```
```

`> (sum-odd 5)`

25

The normal way to perform a frequency count of characters in a string is to initialize an array of character counts to zeroes, increment the count of the array position corresponding to the character for each character that is seen, then collect the non-zero counts in the output. That’s a little bit ugly in Scheme, because array access and string access functions have such long names:

`(define (freq str)`

(let ((counts (make-vector 256 0)))

(do ((i 0 (+ i 1)))

((= i (string-length str)))

(let ((c (char->integer (string-ref str i))))

(vector-set! counts c (+ (vector-ref counts c) 1))))

(do ((i 0 (+ i 1))) ((= 256 i))

(when (positive? (vector-ref counts i))

(display (integer->char i)) (display #\tab)

(display (vector-ref counts i)) (newline)))))

```
```

`> (freq "hello")`

e 1

h 1

l 2

o 1

A more Schemely solution converts the string to a list of characters, sorts them, and collects the non-zero counts; we use the `uniq-c`

function from the Standard Prelude:

`(define (freq str)`

(uniq-c char=? (sort char<? (string->list str))))

```
```

`> (freq "hello")`

((#\e . 1) (#\h . 1) (#\l . 2) (#\o . 1))

The Caesar cipher uses modular arithmetic to shift letters up and down the alphabet; we replace in-place any upper-case letters and leave everythihg else intact:

`(define (caesar n str)`

(define (rotate c)

(if (not (char<=? #\A c #\Z)) c

(integer->char (+ (modulo (+ (char->integer c) n -65) 26) 65))))

(list->string (map rotate (string->list str))))

```
```

`> (caesar 5 "ATTACK AT DAWN")`

FYYFHP FY IFBS

> (caesar -5 "FYYFHP FY IFBS")

"ATTACK AT DAWN"

Note that enciphering and deciphering use the same function but negate the shift.

You can run the program at http://ideone.com/MCsvSa.

Scala:

I hope this isn’t another formatting disaster, but I flipped out thirty years ago when a homework exercise assumed ASCII encoding without comment (one of my worst overreactions, I walked out of the whole course when my solution was criticized for not making that unstated assumption), and I feel the need of a friendly objection today when a suggested solution assumes that any character fits in an octet. Here’s counting UTF-8 Greek letters from today’s Βικιπαίδεια front page :) In Python 3, copy-pasting in Lubuntu to Firefox from an Emacs running in Red Hat over SSH. Who do we blame if it gets mangled? Everything looks fine to me at the moment, a second before posting.

The output:

1 in SML, 2 and 3 in Perl:

A Haskell version, using part of Jussi Piitulainen’s text for the character frequency problem.

EX1.

#include

int sum(int n)

{

if(n==1)

return 1;

else

return (2*n -1)+sum(n-1);

}

int main()

{

int n;

printf(“Enter the no of odd integer needed\n”);

scanf(“%d”,&n);

printf(“Result = %d\n”,sum(n));

}

if you don’t want to use recursive function then simplest method is

#include

int main()

{

int n;

printf(“Enter the no of odd integer needed\n”);

scanf(“%d”,&n);

printf(“Result = %d\n”,n*n);

}

#include

int main()

{

char str[20];

int i,j,count=1;

printf(“Enter the string\n”);

scanf(“%s”,str);

for(i=0;str[i];i++)

{

for(j=0;j

%d\n”,str[i],count);count=1;

}

}

EX 3.

#include

int main()

{

char str[20];

int i;

printf(“Enter the string”);

scanf(“%[^\n]”,str);

for(i=0;str[i];i++)

{

if(str[i]==’ ‘)

continue;

if((str[i]+5)>90)

str[i] =(str[i]+5-26);

else

str[i] = str[i]+5;

}

printf(“%s\n”,str);

}