Skip to content
23 changes: 23 additions & 0 deletions solutions/go/annalyns-infiltration/1/annalyns_infiltration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package annalyn

// CanFastAttack can be executed only when the knight is sleeping.
func CanFastAttack(knightIsAwake bool) bool {
return !knightIsAwake
}

// CanSpy can be executed if at least one of the characters is awake.
func CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake bool) bool {
return knightIsAwake || archerIsAwake || prisonerIsAwake
}

// CanSignalPrisoner can be executed if the prisoner is awake and the archer is sleeping.
func CanSignalPrisoner(archerIsAwake, prisonerIsAwake bool) bool {
return !archerIsAwake && prisonerIsAwake
}

// CanFreePrisoner can be executed if the prisoner is awake and the other 2 characters are asleep
// or if Annalyn's pet dog is with her and the archer is sleeping.
func CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent bool) bool {
return (prisonerIsAwake && !knightIsAwake && !archerIsAwake) || (petDogIsPresent && !archerIsAwake)
}

53 changes: 53 additions & 0 deletions solutions/go/blackjack/1/blackjack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package blackjack

// ParseCard returns the integer value of a card following blackjack ruleset.
func ParseCard(card string) int {
switch card {
case "ace":
return 11
case "two":
return 2
case "three":
return 3
case "four":
return 4
case "five":
return 5
case "six":
return 6
case "seven":
return 7
case "eight":
return 8
case "nine":
return 9
case "ten", "jack", "queen", "king":
return 10
default:
return 0
}
}


// FirstTurn returns the decision for the first turn, given two cards of the
// player and one card of the dealer.
func FirstTurn(card1, card2, dealerCard string) string {
value := ParseCard(card1) + ParseCard(card2)
if ParseCard(card1) == 11 && ParseCard(card2) == 11{
return "P"
}else if value == 21 {
if ParseCard(dealerCard) >= 10{
return "S"
}
return "W"
}else if value<=20 && value>=17 {
return "S"
}else if value<=16 && value >=12 {
if ParseCard(dealerCard) >= 7{
return "H"
}
return "S"
}else{
return "H"
}
}
54 changes: 54 additions & 0 deletions solutions/go/booking-up-for-beauty/1/booking_up_for_beauty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package booking

import (
"fmt"
"time"
)

// Schedule returns a time.Time from a string containing a date.
func Schedule(date string) time.Time {
layout := "1/2/2006 15:04:05"
t, err := time.Parse(layout, date)
if err != nil {
return time.Time{}
}
return t
}

// HasPassed returns whether a date has passed.
func HasPassed(date string) bool {
layout := "January 2, 2006 15:04:05"
t, err := time.Parse(layout, date)
if err != nil {
return false
}
return t.Before(time.Now())
}

// IsAfternoonAppointment returns whether a time is in the afternoon.
func IsAfternoonAppointment(date string) bool {
layout := "Monday, January 2, 2006 15:04:05"
t, err := time.Parse(layout, date)
if err != nil {
return false
}
hour := t.Hour()
return hour >= 12 && hour < 18
}

// Description returns a formatted string of the appointment time.
func Description(date string) string {
layout := "1/2/2006 15:04:05"
t, err := time.Parse(layout, date)
if err != nil {
return "Invalid date format"
}
formatted := t.Format("Monday, January 2, 2006, at 15:04")
return fmt.Sprintf("You have an appointment on %s.", formatted)
}

// AnniversaryDate returns a Time with this year's anniversary on Sept 15 at 00:00 UTC.
func AnniversaryDate() time.Time {
year := time.Now().Year()
return time.Date(year, time.September, 15, 0, 0, 0, 0, time.UTC)
}
21 changes: 21 additions & 0 deletions solutions/go/cars-assemble/1/cars_assemble.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cars

// CalculateWorkingCarsPerHour calculates how many working cars are
// produced by the assembly line every hour.
func CalculateWorkingCarsPerHour(productionRate int, successRate float64) float64 {
var dec float64 = successRate/100
return dec * float64(productionRate)
}

// CalculateWorkingCarsPerMinute calculates how many working cars are
// produced by the assembly line every minute.
func CalculateWorkingCarsPerMinute(productionRate int, successRate float64) int {
return int(CalculateWorkingCarsPerHour(productionRate, successRate)/float64(60))
}

// CalculateCost works out the cost of producing the given number of cars.
func CalculateCost(carsCount int) uint {
div := uint(carsCount/10)
rem := uint(carsCount%10)
return div*95000 + rem*10000
}
70 changes: 70 additions & 0 deletions solutions/go/gross-store/1/gross_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gross

// Units stores the Gross Store unit measurements.
func Units() map[string]int {
units := make(map[string]int)

units["quarter_of_a_dozen"] = 3
units["half_of_a_dozen"] = 6
units["dozen"] = 12
units["small_gross"] = 120
units["gross"] = 144
units["great_gross"] = 1728

return units
}

// NewBill creates a new bill.
func NewBill() map[string]int {
bill := make(map[string]int)
return bill
}


// AddItem adds an item to customer bill.
func AddItem(bill, units map[string]int, item, unit string) bool {
unitValue, unitExists := units[unit]
if !unitExists {
return false
}
bill[item] += unitValue // add to existing quantity
return true
}


// RemoveItem removes an item from customer bill.
func RemoveItem(bill, units map[string]int, item, unit string) bool {
itemValue, itemExists := bill[item]
unitValue, unitExists := units[unit]

// Check if item exists in bill and unit exists in units map
if !itemExists || !unitExists {
return false
}

newValue := itemValue - unitValue

if newValue < 0 {
// Can't have negative quantity
return false
} else if newValue == 0 {
// Remove item from bill
delete(bill, item)
return true
} else {
// Reduce item quantity
bill[item] = newValue
return true
}
}


// GetItem returns the quantity of an item that the customer has in his/her bill.
func GetItem(bill map[string]int, item string) (int, bool) {
quantity, exists := bill[item]
if !exists {
return 0, false
}
return quantity, true
}

6 changes: 6 additions & 0 deletions solutions/go/hello-world/1/hello_world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package greeting

// HelloWorld greets the world.
func HelloWorld() string {
return "Hello, World!"
}
19 changes: 19 additions & 0 deletions solutions/go/lasagna/1/lasagna.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lasagna

// OvenTime constant defines the expected oven time in minutes
const OvenTime = 40

// RemainingOvenTime returns the remaining minutes based on the `actual` minutes already in the oven.
func RemainingOvenTime(actualMinutesInOven int) int {
return OvenTime - actualMinutesInOven
}

// PreparationTime calculates the time needed to prepare the lasagna based on the amount of layers.
func PreparationTime(numberOfLayers int) int {
return numberOfLayers * 2
}

// ElapsedTime calculates the total time spent preparing and baking the lasagna.
func ElapsedTime(numberOfLayers, actualMinutesInOven int) int {
return PreparationTime(numberOfLayers) + actualMinutesInOven
}
20 changes: 20 additions & 0 deletions solutions/go/party-robot/1/party_robot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package partyrobot

import "fmt"

// Welcome greets a person by name.
func Welcome(name string) string {
return fmt.Sprintf("Welcome to my party, %s!", name)
}

func HappyBirthday(name string, age int) string {
return fmt.Sprintf("Happy birthday %s! You are now %d years old!", name, age)
}

func AssignTable(name string, table int, neighbor, direction string, distance float64) string {
return fmt.Sprintf(
"Welcome to my party, %s!\nYou have been assigned to table %03d. Your table is %s, exactly %.1f meters from here.\nYou will be sitting next to %s.",
name, table, direction, distance, neighbor,
)
}

32 changes: 32 additions & 0 deletions solutions/go/vehicle-purchase/1/vehicle_purchase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package purchase
import "fmt"

// NeedsLicense determines whether a license is needed to drive a type of vehicle. Only "car" and "truck" require a license.
func NeedsLicense(kind string) bool {
if kind == "car" || kind == "truck" {
return true
}
return false
}

// ChooseVehicle recommends a vehicle for selection. It always recommends the vehicle that comes first in lexicographical order.
func ChooseVehicle(option1, option2 string) string {
var chosen string
if option1 < option2 {
chosen = option1
} else {
chosen = option2
}
return fmt.Sprintf("%s is clearly the better choice.", chosen)
}

// CalculateResellPrice calculates how much a vehicle can resell for at a certain age.
func CalculateResellPrice(originalPrice, age float64) float64 {
if age < 3 {
return originalPrice * 0.8
} else if age >= 10 {
return originalPrice * 0.5
} else {
return originalPrice * 0.7
}
}
13 changes: 13 additions & 0 deletions solutions/go/weather-forecast/1/weather_forecast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//Package weather provides tools to get forecast.
package weather

//CurrentCondition stores the current condition.
var CurrentCondition string
//CurrentLocation stores the current location.
var CurrentLocation string

//Forecast returns weather forecast.
func Forecast(city, condition string) string {
CurrentLocation, CurrentCondition = city, condition
return CurrentLocation + " - current weather condition: " + CurrentCondition
}
7 changes: 7 additions & 0 deletions solutions/javascript/hello-world/1/hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// This is only a SKELETON file for the 'Hello World' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
export function hello() {
return 'Hello, World!';
}
2 changes: 2 additions & 0 deletions solutions/python/hello-world/1/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello():
return "Hello, World!"