More puzzlers
This commit is contained in:
@@ -9,7 +9,7 @@ inline fun array(vararg array : Long) = array
|
||||
inline fun array(vararg array : Double) = array
|
||||
inline fun array(vararg array : Float) = array
|
||||
|
||||
fun Any?.identityEquals(other : Any?) = this === other
|
||||
//fun Any?.identityEquals(other : Any?) = this === other
|
||||
|
||||
inline fun <T : Any> T?.sure() : T {
|
||||
if (this == null)
|
||||
|
||||
@@ -12,5 +12,5 @@ fun main(args : Array<String>) {
|
||||
println("Animals are equal: " + (pig == dog));
|
||||
|
||||
// Note:
|
||||
println("Animals are equal: " + (pig identityEquals dog));
|
||||
println("Animals are equal: " + (pig identityEquals dog))
|
||||
}
|
||||
@@ -25,4 +25,4 @@ fun main(args : Array<String>) {
|
||||
// nipper.bark()
|
||||
Dog.bark()
|
||||
Basenji.bark()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package _06_classy._53_Do_Your_Thing;
|
||||
|
||||
public class MyThing extends Thing {
|
||||
private final int arg;
|
||||
|
||||
/*
|
||||
* This constructor is illegal. Rewrite it so that it has the same
|
||||
* effect but is legal.
|
||||
*/
|
||||
public MyThing() {
|
||||
this((int)System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/*
|
||||
* This constructor is illegal. Rewrite it so that it has the same
|
||||
* effect but is legal.
|
||||
*/
|
||||
public MyThing(int arg) {
|
||||
super(arg);
|
||||
this.arg = arg;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace `do`.your.thing
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
import _06_classy._53_Do_Your_Thing.Thing
|
||||
|
||||
class MyThing(val arg : Int) : Thing(arg) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
println(MyThing(10).arg)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package _06_classy._53_Do_Your_Thing;
|
||||
|
||||
// This is meant to represent a library class. You must not modify it.
|
||||
public class Thing {
|
||||
public Thing(int i) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package _06_classy._54_Null_and_Void;
|
||||
|
||||
public class Null {
|
||||
public static void greet() {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
((Null) null).greet();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace `null`.and.void
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
|
||||
class Null() {
|
||||
class object {
|
||||
fun greet() {
|
||||
println("Hello world!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// The problemati code does not compile:
|
||||
// (null as Null).greet()
|
||||
Null.greet()
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package _06_classy._55_Creationism;
|
||||
|
||||
public class Creator {
|
||||
public static void main(String[] args) {
|
||||
// for (int i = 0; i < 100; i++)
|
||||
// Creature creature = new Creature();
|
||||
System.out.println(Creature.numCreated());
|
||||
}
|
||||
}
|
||||
|
||||
class Creature {
|
||||
private static long numCreated = 0;
|
||||
|
||||
public Creature() {
|
||||
numCreated++;
|
||||
}
|
||||
|
||||
public static long numCreated() {
|
||||
return numCreated;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace creationism
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
|
||||
class Creature() {
|
||||
class object {
|
||||
var numCreated = 0
|
||||
private set
|
||||
}
|
||||
|
||||
{
|
||||
// Creature.numCreated++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// for (i in 1..100)
|
||||
// val creature = Creature()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package _07_library._56_Big_Problem;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class BigProblem {
|
||||
public static void main(String[] args) {
|
||||
BigInteger fiveThousand = new BigInteger("5000");
|
||||
BigInteger fiftyThousand = new BigInteger("50000");
|
||||
BigInteger fiveHundredThousand
|
||||
= new BigInteger("500000");
|
||||
|
||||
BigInteger total = BigInteger.ZERO;
|
||||
total.add(fiveThousand);
|
||||
total.add(fiftyThousand);
|
||||
total.add(fiveHundredThousand);
|
||||
System.out.println(total);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace big.problem
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
import java.math.BigInteger
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
val fiveThousand = "5000".bi()
|
||||
val fiftyThousand = "50000".bi()
|
||||
val fiveHundredThousand = "500000".bi()
|
||||
|
||||
val total : BigInteger = "0".bi()//BigInteger.ZERO
|
||||
total + fiveThousand
|
||||
total + fiftyThousand
|
||||
total + fiveHundredThousand
|
||||
println(total) // No surprise
|
||||
|
||||
var total1 : BigInteger = "0".bi()//BigInteger.ZERO
|
||||
total1 += fiveThousand
|
||||
total1 += fiftyThousand
|
||||
total1 += fiveHundredThousand
|
||||
println(total1) // Works
|
||||
}
|
||||
|
||||
inline fun String.bi() : BigInteger = BigInteger(this)
|
||||
inline fun BigInteger.plus(other : BigInteger) = this add other
|
||||
@@ -0,0 +1,15 @@
|
||||
package _07_library._59_Whats_the_Difference;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Differences {
|
||||
public static void main(String[] args) {
|
||||
int vals[] = { 789, 678, 567, 456, 345, 234, 123, 012 };
|
||||
Set<Integer> diffs = new HashSet<Integer>();
|
||||
|
||||
for (int i = 0; i < vals.length; i++)
|
||||
for (int j = i; j < vals.length; j++)
|
||||
diffs.add(vals[i] - vals[j]);
|
||||
System.out.println(diffs.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace whats.the.difference
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
import java.util.*
|
||||
|
||||
fun iarray(vararg a : Int) = a // BUG
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// Problematic code does not compile
|
||||
// val vals = iarray(789, 678, 567, 456, 345, 234, 123, 012)
|
||||
val vals = iarray(789, 678, 567, 456, 345, 234, 123, 12)
|
||||
val diffs = HashSet<Int>
|
||||
for (i in vals.indices)
|
||||
for (j in i..vals.lastIndex())
|
||||
diffs.add(vals[i] - vals[j])
|
||||
println(diffs.size())
|
||||
}
|
||||
|
||||
fun IntArray.lastIndex() = size - 1
|
||||
Reference in New Issue
Block a user