Skip to content

Commit 9f760cc

Browse files
initial commit
1 parent 73c3f38 commit 9f760cc

File tree

11 files changed

+357
-0
lines changed

11 files changed

+357
-0
lines changed

.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/KotlinJavaRuntime.xml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+241
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

elementsOfProgrammingInterviews.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
11+
</component>
12+
</module>

src/Main.kt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
fun main(args: Array<String>){
3+
4+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ch5PrimitiveTypes
2+
3+
import kotlin.math.max
4+
import kotlin.math.min
5+
6+
fun main(args: Array<String>) {
7+
// test if two rectangles intersect, if they do return the intersecting rectangle
8+
// assume that if only sides are touching there is no intersection
9+
10+
val rect1 = Rect(0, 2, 0, 2)
11+
val rect2 = Rect(1, 4, 1, 4)
12+
println(rect1.doesIntersect(rect2))
13+
println(rect1.intersectedRect(rect2))
14+
}
15+
16+
data class Rect(val x: Int,
17+
val width: Int,
18+
val y: Int,
19+
val height: Int){
20+
21+
fun doesIntersect(other: Rect): Boolean{
22+
return x + width > other.x && y + height < other.y + height
23+
}
24+
25+
fun intersectedRect(other: Rect): Rect{
26+
return Rect(x = max(x, other.x),
27+
width = min(x + width, other.x + width) - max(x, other.x),
28+
y = max(y, other.y),
29+
height = min(y + height, other.y + height) - max(y, other.y))
30+
}
31+
}
32+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ch5PrimitiveTypes
2+
3+
fun main(args: Array<String>) {
4+
val input = -987654321
5+
println(input)
6+
println(reverseIt(input))
7+
}
8+
9+
fun reverseIt(num: Int): Int {
10+
val isNegative = num < 0
11+
var original = Math.abs(num)
12+
var result = 0
13+
14+
while (original > 0){
15+
result = result * 10 + original.rem(10)
16+
original /= 10
17+
18+
}
19+
20+
return if(isNegative) -result else result
21+
}
22+
23+
// takes more memory
24+
fun bruteForce(num: Int): String {
25+
val inString = num.toString()
26+
return inString.reversed()
27+
}
28+

0 commit comments

Comments
 (0)