Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds lesson 08 content #224

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lesson_08/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Lesson 08

## Homework

* Complete three [coding interview questions](#coding-interview-questions).

## Coding Interview questions

* Find a cycle in a LinkedList.
* Complete function that returns whether an array of elements contains a duplicate.
* Write a function that returns the maximum path of a tree.

9 changes: 9 additions & 0 deletions lesson_08/collections/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions lesson_08/collections/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
64 changes: 64 additions & 0 deletions lesson_08/collections/collections_app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
eclipse
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.2.2"
id("com.adarshr.test-logger") version "4.0.0"
}

apply(plugin = "io.spring.dependency-management")

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation("com.codedifferently.instructional:instructional-lib")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.assertj:assertj-core:3.25.1")
testImplementation("at.favre.lib:bcrypt:0.10.2")

// This dependency is used by the application.
implementation("com.codedifferently.instructional:instructional-lib")
implementation("com.google.guava:guava:31.1-jre")
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.projectlombok:lombok:1.18.30")
implementation("org.springframework.boot:spring-boot-starter")
}

application {
// Define the main class for the application.
mainClass.set("com.codedifferently.lesson8.Lesson8")
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}


configure<com.diffplug.gradle.spotless.SpotlessExtension> {

format("misc", {
// define the files to apply `misc` to
target("*.gradle", ".gitattributes", ".gitignore")

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
})

java {
// don't need to set target, it is inferred from java

// apply a specific flavor of google-java-format
googleJavaFormat()
// fix formatting of type annotations
formatAnnotations()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.codedifferently.lesson8;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication(scanBasePackages = "com.codedifferently")
public class Lesson8 {

public static void main(String[] args) {
var application = new SpringApplication(Lesson8.class);
application.run(args);
}

public static boolean containsCycle(LinkedListNode head) {
return false;
}

public static int getMaximumBinaryTreeHeight(BinaryTreeNode root) {
return -1;
}

public static boolean containsDuplicates(String[] values) {
return false;
}

public static class LinkedListNode {
public LinkedListNode next;
public int val;

public LinkedListNode(int val) {
this.val = val;
}
}

public static class BinaryTreeNode {
public int val;
public BinaryTreeNode left;
public BinaryTreeNode right;

public BinaryTreeNode(int val) {
this.val = val;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.codedifferently.lesson8;

import static org.assertj.core.api.Assertions.assertThat;
import static com.codedifferently.lesson8.Lesson8.LinkedListNode;
import static com.codedifferently.lesson8.Lesson8.BinaryTreeNode;

import org.junit.jupiter.api.Test;

class Lesson8Test {

@Test
void testCanVote() {
assertThat(new Lesson8()).isNotNull();
}

@Test
void testContainsCycle() {
LinkedListNode node1 = new LinkedListNode(1);
LinkedListNode node2 = new LinkedListNode(2);
LinkedListNode node3 = new LinkedListNode(3);
node1.next = node2;
node2.next = node3;
node3.next = node1;

boolean result = Lesson8.containsCycle(node1);

assertThat(result).isTrue();
}

@Test
void testGetMaximumBinaryTreeHeight() {
BinaryTreeNode root = new BinaryTreeNode(1);
BinaryTreeNode left = new BinaryTreeNode(2);
BinaryTreeNode right = new BinaryTreeNode(3);
root.left = left;
root.right = right;
left.left = new BinaryTreeNode(4);
left.right = new BinaryTreeNode(5);
right.left = new BinaryTreeNode(6);

int result = Lesson8.getMaximumBinaryTreeHeight(root);

assertThat(result).isEqualTo(3);
}

@Test
void testContainsDuplicates() {
String[] values = {"apple", "banana", "cherry", "apple"};

boolean result = Lesson8.containsDuplicates(values);

assertThat(result).isTrue();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists