Skip to content

Instantly share code, notes, and snippets.

@pathawks
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathawks/c67db2d91faedea0c81b to your computer and use it in GitHub Desktop.
Save pathawks/c67db2d91faedea0c81b to your computer and use it in GitHub Desktop.
Programming Challenge 1
REM To run:
REM 1. Get a time machine
REM 2. Travel back to the early 90s
REM 3. In MS-DOS 5+, run `qbasic /run CHALLEN1.BAS`
10 PRINT "Enter a whole number between 1000 and 999999 without spaces or commas."
20 INPUT "", N&
30 IF N& < 1000 OR N& > 999999 THEN GOTO 10
40 PRINT LTRIM$(STR$(INT(N& / 1000)));
50 PRINT ",";
60 LET N = N& MOD 1000
70 IF N < 100 THEN GOTO 100
80 PRINT LTRIM$(STR$(N))
90 SYSTEM
100 IF N < 10 THEN PRINT "0";
110 PRINT "0";
120 GOTO 80
/**
* To compile:
* gcc Challenge1.c -Wall -o Challenge1
*/
#include <limits.h>
#include <stdio.h>
const long MIN_NUM = 1000, MAX_NUM = ULONG_MAX;
const int BUFFER_SIZE = 27; // log10(ULONG_MAX)+log10(ULONG_MAX)/3+1
const char *PROMPT_STRING = "Enter a whole number between %lu and %lu\nwithout spaces or commas.\n";
const char *SCAN_FORMAT = "%lu";
const char *OUTPUT_FORMAT = "%s\n";
const char ASCII_ZERO = '0';
void formatNumber( unsigned long inNum, char *numString ) {
unsigned long digits;
int commaPosition, position;
position = 0;
digits = inNum;
while ( digits ) {
++position;
digits /= 10;
}
position += (position-1) / 3; // How many commas we will need
numString[position] = 0; // null terminator
commaPosition = position - 4; // three digits + null terminator
while ( position-- ) {
if ( position == commaPosition && position > 0 ) {
numString[position--] = ',';
commaPosition -= 4; // three digits + position of current comma
}
numString[position] = inNum % 10 + ASCII_ZERO;
inNum /= 10;
}
return;
}
int main( void ) {
unsigned long inNum;
char numString[BUFFER_SIZE];
do {
printf( PROMPT_STRING, MIN_NUM, MAX_NUM );
scanf( SCAN_FORMAT, &inNum );
while ( getchar() != '\n' ) {} // Flush input buffer
} while ( inNum < MIN_NUM );
// printf( "%lu,%.3lu\n", inNum/1000, inNum%1000 );
formatNumber( inNum, numString );
printf( OUTPUT_FORMAT, numString );
return 0;
}
/**
* To compile:
* g++ Challenge1.cpp -Wall -o Challenge1
*/
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
using namespace std;
struct formatNumber : numpunct<char> {
string do_grouping() const {return "\03";}
};
int main( void ) {
const string PROMPT_STRING =
"Enter a whole number between 1000 and 999999 without spaces or commas.";
int inNum;
do {
cout << PROMPT_STRING << endl;
cin >> inNum;
} while ( inNum < 1000 || inNum > 999999 );
locale loc( cout.getloc(), new formatNumber );
cout.imbue( loc );
cout << inNum << endl;
return 0;
}
/**
* To compile:
* javac -Xlint:all Challenge1.java
*/
import java.util.Scanner;
public class Challenge1 {
public static void main( String[] args ) {
final String PROMPT_STRING = "Enter a whole number between %d and %d "
+ "without spaces or commas.\n";
final String FORMAT_STRING = "%,d\n";
final int MIN_NUM = 1000, MAX_NUM = 999999;
Scanner in = new Scanner( System.in );
int inNum;
do {
System.out.printf( PROMPT_STRING, MIN_NUM, MAX_NUM );
inNum = in.nextInt();
} while ( inNum < MIN_NUM || inNum > MAX_NUM );
// The Right Way
System.out.printf( FORMAT_STRING, inNum );
// System.out.printf( "%d,%3d\n", inNum/1000, inNum%1000 );
/* System.out.print( inNum/1000 );
* System.out.print( "," );
* if( inNum%1000 < 100 ) // Print some leading zeros?
* System.out.print( inNum%1000<10 ? "00" : "0" );
* System.out.println( inNum%1000 );
*/
in.close();
return;
}
}
/*
* See this in action:
* http://student.pathawks.com/AICSC/Challenges/20150202/
*/
"use strict";
// Throughout this script, I use the idom x|0 to dynamically cast x to an int
var formatNum = function( inNum ) {
if ( inNum >= 1000 && inNum <= 999999 ) {
var lastThree = inNum%1000;
return ((inNum/1000)|0)+","+(lastThree<100?(lastThree<10?"00":"0"):"")+lastThree;
}
return false;
};
#!/usr/bin/env python
#
# To run:
# python Challenge1.py
MIN_NUM = 1000 # Python does not actually enforce constancy, but hopefully the
MAX_NUM = 999999 # SCREAMING_SNAKE_CASE is a hint to develoopers not to modify
# these variables
PROMPT_STRING = '''\
Enter a whole number between {min} and {max} without spaces or commas.
'''.format( min=MIN_NUM, max=MAX_NUM )
FORMAT_STRING = "{:,}"
inNum = None
while True: # What I'm really after is a `do while` loop
try:
inNum = int( input( PROMPT_STRING ) )
if inNum >= MIN_NUM and inNum <= MAX_NUM:
break
except: # If the input cannot be converted to an int:
pass
print( FORMAT_STRING.format( inNum ) )
#!/usr/bin/env ruby
#
# To run:
# ruby Challenge1.rb
MIN_NUM = 1000 # Ruby does not actually enforce constancy, but will produce a
MAX_NUM = 999999 # warning if these variables are later modified
PROMPT_STRING = """\
Enter a whole number between #{MIN_NUM} and #{MAX_NUM} without spaces or commas.
"""
FORMAT_STRING = "%d,%.3d\n"
begin
puts PROMPT_STRING
@inNum = gets.to_i
end while @inNum < MIN_NUM or @inNum > MAX_NUM
puts FORMAT_STRING%[ @inNum/1000, @inNum%1000 ]
/**
* To compile:
* g++ Challenge1.s -Wall -o Challenge1
*/
.code32
.equ inNum, 8
.equ _main, main
.equ printf, _printf
.equ scanf, _scanf
.globl _main
.equ MIN_NUMBER, 1000
.equ MAX_NUMBER, 999999
.data
PROMPT_STRING:
.asciz "Enter a whole number between 1000 and 999999 without spaces or commas.\n"
FORMAT_STRING:
.asciz "%d,%.3d\n"
INPUT_STRING:
.asciz "%d"
.text
main:
subl $12, %esp
leal inNum(%esp), %eax
movl %eax, 4(%esp)
inputLoop:
movl $PROMPT_STRING, (%esp)
call printf
movl $INPUT_STRING, (%esp)
call scanf
movl inNum(%esp), %eax
cmpl $MIN_NUMBER, %eax
jl inputLoop
cmpl $MAX_NUMBER, %eax
jg inputLoop
xorl %edx, %edx
movl $MIN_NUMBER, %ecx
divl %ecx
movl %edx, 8(%esp)
movl %eax, 4(%esp)
movl $FORMAT_STRING, (%esp)
call printf
addl $12, %esp
xorl %eax, %eax # Return 0
ret
#!/usr/bin/swift
/*
* To run:
* swift Challenge1.swift
* Or to compile:
* swiftc -sdk $(xcrun --show-sdk-path --sdk macosx) Challenge1.swift
*/
import Foundation;
let formatter = NSNumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSeparator = ","
let stdin = NSFileHandle.fileHandleWithStandardInput()
let MIN_NUM = 1000
let MAX_NUM = 999999
let PROMPT_STRING = "Enter a whole number between \(MIN_NUM) and \(MAX_NUM) without spaces or commas."
var inNum: Int
var outString: String
func input() -> Int {
let string = NSString( data: stdin.availableData, encoding:NSUTF8StringEncoding )
return string?.integerValue ?? 0
}
do {
println( PROMPT_STRING )
inNum = input()
} while ( inNum < MIN_NUM || inNum > MAX_NUM )
outString = formatter.stringFromNumber( inNum )!
println( outString )
stdin.closeFile()
/**
* To compile:
* javac -Xlint:all Challenge1HardMode.java
*/
import java.util.Scanner;
public class Challenge1HardMode {
static final long MIN_NUM = 1000, MAX_NUM = Long.MAX_VALUE;
static final String PROMPT_STRING = "Enter a whole number between %d"
+ " and %d\nwithout spaces or commas.\n";
public static String formatNumber( long inNum ) {
String numString = "";
int commaPosition = 4;
while ( inNum != 0 ) {
if ( --commaPosition == 0 ) {
numString = ',' + numString;
commaPosition = 3;
}
numString = (char) (inNum % 10 + '0') + numString;
inNum /= 10;
}
return numString;
}
public static void main( String[] args ) {
Scanner in = new Scanner( System.in );
long inNum;
do {
System.out.printf( PROMPT_STRING, MIN_NUM, MAX_NUM );
inNum = in.nextLong();
} while ( inNum < MIN_NUM || inNum > MAX_NUM );
System.out.println( formatNumber( inNum ) );
in.close();
return;
}
}
@pathawks
Copy link
Author

My first Swift code 🎈
It's not pretty, but it compiles.

@ChadH1971
Copy link

Nice work Pat. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment