## Third Biggest Number

### April 13, 2018

Our program reads ten numbers in a loop, stores them in a list, then sorts the list and returns the third largest, with appropriate prompts:

(define (third-biggest-number) (let loop ((n 10) (xs (list))) (cond ((positive? n) (display "Enter a number: ") (loop (- n 1) (cons (read) xs))) (else (display "The third biggest number is ") (display (caddr (sort > xs))) (newline)))))

> (third-biggest-number) Enter a number: 14 Enter a number: 23 Enter a number: 102 Enter a number: 97 Enter a number: 29 Enter a number: 4 Enter a number: 19 Enter a number: 85 Enter a number: 53 Enter a number: 111 The third biggest number is 97

Here’s another version of the program, written in a more imperative style:

(define (third-biggest-number) (let ((n 10) (xs (list))) (while (positive? n) (display "Enter a number: ") (set! n (- n 1)) (set! xs (cons (read) xs))) (display "The third biggest number is ") (display (caddr (sort > xs))) (newline)))

You can run the program at https://ideone.com/H9Itwr.

Advertisements

Pages: 1 2

Alternate solution keeps track of the three largest values using an array:

Mumps solutions

USER>k

USER>d 3rd^zjsg

Enter number: 999999

Enter number: 10000

Enter number: 2345

Enter number: 781

Enter number: 56

Enter number: 1

Enter number: 2

Enter number: 99

Enter number: 678

Enter number: 3456

3rd number is 56

A Haskell version.

Python 3

Sample output:

[76631, 719928, 63284, 348692, 521266, 687164, 663331, 845208, 466387, 33721]

[687164]

Ruby. Assumes clean input of positive integers. This is a trick question right?

My previous entry is invalid Ruby. This one does the trick.

(1..10 | % { (Read-Host -Prompt “Enter a number”) -as [int] } | Sort -Unique)[-3]

A translation of my Ruby version in Go.

package main

import (

"fmt"

"sort"

)

func main() {

numbers := make([]int, 10)

// Get numbers from user

for i := range numbers {

fmt.Print("Enter a number: ")

fmt.Scan(&numbers[i])

}

// Remove duplicates

uniqNumbers := []int{}

m := make(map[int]bool)

for _, x := range numbers {

m[x] = true

}

for k, _ := range m {

uniqNumbers = append(uniqNumbers, k)

}

if len(uniqNumbers) < 3 {

fmt.Println("Nop")

return

}

// Sort in descending order

sort.Sort(sort.Reverse(sort.IntSlice(numbers)))

fmt.Println("Third lagest is:", numbers[2])

}

A classic interview answer is to use a min-heap of fixed-size:

Kotlin with a recursion approach. Give the first parameter

fun main(args: Array) {

var myList = mutableListOf(1,65,89,45,32,75,11,32,82,11)

println(recThirdBig(myList,null,null,null))

}

tailrec fun recThirdBig(testList: List, oneBig: Int?, twoBig: Int?, threeBig: Int?): Int? {

if (testList.isEmpty()) return threeBig

if (oneBig == null || testList[0] > oneBig) return recThirdBig(testList.drop(1), testList[0], oneBig, twoBig)

if (twoBig == null || testList[0] > twoBig) return recThirdBig(testList.drop(1), oneBig, testList[0], twoBig)

if (threeBig == null || testList[0] > threeBig) return recThirdBig(testList.drop(1), oneBig, twoBig, testList[0])

return recThirdBig(testList.drop(1), oneBig, twoBig, threeBig)

}

This is similar to a prior problem.

Here’s a solution in C.

Example: