primeiro commit com exercicios de Exercism realizados até esta data

This commit is contained in:
2026-10-17 22:10:27 +01:00
commit 58fccbe27a
708 changed files with 35038 additions and 0 deletions
@@ -0,0 +1,26 @@
{
"authors": [
"oanaOM"
],
"files": {
"solution": [
"annalyns_infiltration.go"
],
"test": [
"annalyns_infiltration_test.go"
],
"exemplar": [
".meta/exemplar.go"
],
"invalidator": [
"go.mod"
]
},
"forked_from": [
"csharp/annalyns-infiltration"
],
"blurb": "Learn about booleans by helping Annalyn free her best friend.",
"custom": {
"taskIdsEnabled": true
}
}
@@ -0,0 +1 @@
{"track":"go","exercise":"annalyns-infiltration","id":"7a1da8b9a1844d21a3cc71aca4ee5f0b","url":"https://exercism.org/tracks/go/exercises/annalyns-infiltration","handle":"sanctushck","is_requester":true,"auto_approve":false}
+41
View File
@@ -0,0 +1,41 @@
# Help
## Running the tests
To run the tests run the command `go test` from within the exercise directory.
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
flags:
go test -v --bench . --benchmem
Keep in mind that each reviewer will run benchmarks on a different machine, with
different specs, so the results from these benchmark tests may vary.
## Submitting your solution
You can submit your solution using the `exercism submit annalyns_infiltration.go` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
- The [Go track's programming category on the forum](https://forum.exercism.org/c/programming/go)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [How to Write Go Code](https://golang.org/doc/code.html)
- [Effective Go](https://golang.org/doc/effective_go.html)
- [Go Resources](http://golang.org/help)
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)
+23
View File
@@ -0,0 +1,23 @@
# Hints
## General
- There are three [boolean operators][logical operators] to work with boolean values.
- Multiple operators can be combined in a single expression.
## 1. Check if a fast attack can be made
- The logical NOT operator (`!`) can be placed before an expression to negate its value.
## 2. Check if the group can be spied upon
- Logical operators are typically used to evaluate whether two or more expressions are true or not true.
## 3. Check if the prisoner can be signaled
- Logical operators execute in the order of their precedence (from highest to lowest): `!`, `&&`, `||`.
- For more details check out the Operator Precedence section on the [official golang documentation][operators] and the [truth table][truth table].
[logical operators]: https://golang.org/ref/spec#Logical_operators
[operators]: https://golang.org/ref/spec#Operators
[truth table]: https://www.digitalocean.com/community/tutorials/understanding-boolean-logic-in-go
+111
View File
@@ -0,0 +1,111 @@
# Annalyn's Infiltration
Welcome to Annalyn's Infiltration on Exercism's Go Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
Booleans in Go are represented by the predeclared boolean type `bool`, which values can be either `true` or `false`.
It's a defined type.
```go
var closed bool // boolean variable 'closed' implicitly initialized with 'false'
speeding := true // boolean variable 'speeding' initialized with 'true'
hasError := false // boolean variable 'hasError' initialized with 'false'
```
Go supports three logical operators that can evaluate expressions down to Boolean values, returning either `true` or `false`.
| Operator | What it means |
| ----------- | --------------------------------------------- |
| `&&` (and) | It is true if both statements are true. |
| `\|\|` (or) | It is true if at least one statement is true. |
| `!` (not) | It is true only if the statement is false. |
## Instructions
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing.
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest.
Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned.
It turns out there are two kidnappers: a mighty knight and a cunning archer.
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
- _Fast attack_: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
- _Spy_: the group can be spied upon if at least one of them is awake.
Otherwise, spying is a waste of time.
- _Signal prisoner_: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling so they could intercept the message.
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
This is a risky thing to do and can only succeed in one of two ways:
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
## 1. Check if a fast attack can be made
Define the `CanFastAttack()` function that takes a boolean value that indicates if the knight is awake.
This function returns `true` if a fast attack can be made based on the state of the knight.
Otherwise, returns `false`:
```go
var knightIsAwake = true
fmt.Println(CanFastAttack(knightIsAwake))
// Output: false
```
## 2. Check if the group can be spied upon
Define the `CanSpy()` function that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake.
The function returns `true` if the group can be spied upon, based on the state of the three characters.
Otherwise, returns `false`:
```go
var knightIsAwake = false
var archerIsAwake = true
var prisonerIsAwake = false
fmt.Println(CanSpy(knightIsAwake, archerIsAwake, prisonerIsAwake))
// Output: true
```
## 3. Check if the prisoner can be signaled
Define the `CanSignalPrisoner()` function that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake.
The function returns `true` if the prisoner can be signaled, based on the state of the two characters.
Otherwise, returns `false`:
```go
var archerIsAwake = false
var prisonerIsAwake = true
fmt.Println(CanSignalPrisoner(archerIsAwake, prisonerIsAwake))
// Output: true
```
## 4. Check if the prisoner can be freed
Define the `CanFreePrisoner()` function that takes four boolean values.
The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake.
The last parameter indicates if Annalyn's pet dog is present.
The function returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog presence.
Otherwise, it returns `false`:
```go
var knightIsAwake = false
var archerIsAwake = true
var prisonerIsAwake = false
var petDogIsPresent = false
fmt.Println(CanFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent))
// Output: false
```
## Source
### Created by
- @oanaOM
@@ -0,0 +1,22 @@
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)) || (!archerIsAwake && petDogIsPresent)
}
@@ -0,0 +1,286 @@
package annalyn
import "testing"
type charactersState struct {
desc string
knightIsAwake bool
archerIsAwake bool
prisonerIsAwake bool
dogIsPresent bool
expected bool
}
func TestCanFastAttack(t *testing.T) {
tests := []charactersState{
{
desc: "Knight is awake",
knightIsAwake: true,
expected: false,
},
{
desc: "Knight is sleeping",
knightIsAwake: false,
expected: true,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if got := CanFastAttack(tc.knightIsAwake); got != tc.expected {
t.Errorf("CanFastAttack(%v) = %v; want %v", tc.knightIsAwake, got, tc.expected)
}
})
}
}
func TestCanSpy(t *testing.T) {
tests := []charactersState{
{
desc: "All characters are sleeping",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: false,
expected: false,
},
{
desc: "Knight is awake, archer and prisoner are sleeping",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: false,
expected: true,
},
{
desc: "Knight and archer are awake, prisoner is sleeping",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: false,
expected: true,
},
{
desc: "Knight and prisoner are awake, archer is sleeping",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: true,
expected: true,
},
{
desc: "Archer is awake, knight and prisoner are sleeping",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: false,
expected: true,
},
{
desc: "Archer and prisoner are awake, knight is sleeping",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: true,
expected: true,
},
{
desc: "Prisoner is awake, knight and archer are sleeping",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: true,
expected: true,
},
{
desc: "All characters are awake",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: true,
expected: true,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if got := CanSpy(tc.knightIsAwake, tc.archerIsAwake, tc.prisonerIsAwake); got != tc.expected {
t.Errorf("CanSpy(%v, %v, %v) = %v; want %v", tc.knightIsAwake, tc.archerIsAwake, tc.prisonerIsAwake, got, tc.expected)
}
})
}
}
func TestCanSignalPrisoner(t *testing.T) {
tests := []charactersState{
{
desc: "All characters are sleeping",
archerIsAwake: false,
prisonerIsAwake: false,
expected: false,
},
{
desc: "Archer is sleeping, prisoner is awake",
archerIsAwake: false,
prisonerIsAwake: true,
expected: true,
},
{
desc: "Archer is awake, prisoner is sleeping",
archerIsAwake: true,
prisonerIsAwake: false,
expected: false,
},
{
desc: "All characters are awake",
archerIsAwake: true,
prisonerIsAwake: true,
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if got := CanSignalPrisoner(tc.archerIsAwake, tc.prisonerIsAwake); got != tc.expected {
t.Errorf("CanSignalPrisoner(%v, %v) = %v; want %v", tc.archerIsAwake, tc.prisonerIsAwake, got, tc.expected)
}
})
}
}
func TestCanFreePrisoner(t *testing.T) {
tests := []charactersState{
{
desc: "All characters are sleeping. Dog is not present.",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: false,
dogIsPresent: false,
expected: false,
},
{
desc: "All characters are sleeping. Dog is present.",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: false,
dogIsPresent: true,
expected: true,
},
{
desc: "Knight and archer are sleeping. Prisoner is awake. Dog is not present.",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: true,
dogIsPresent: false,
expected: true,
},
{
desc: "Knight and archer are sleeping. Prisoner is awake. Dog is present.",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: true,
dogIsPresent: true,
expected: true,
},
{
desc: "Knight is sleeping. Archer is awake. Prisoner is sleeping. Dog is not present.",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: false,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight is sleeping. Archer is awake. Prisoner is sleeping. Dog is present.",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: false,
dogIsPresent: true,
expected: false,
},
{
desc: "Knight is sleeping. Archer is awake. Prisoner is awake. Dog is not present.",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: true,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight is sleeping. Archer is awake. Prisoner is awake. Dog is present.",
knightIsAwake: false,
archerIsAwake: true,
prisonerIsAwake: true,
dogIsPresent: true,
expected: false,
},
{
desc: "Knight is awake. Archer is sleeping. Prisoner is sleeping. Dog is not present.",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: false,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight is awake. Archer is sleeping. Prisoner is sleeping. Dog is present.",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: false,
dogIsPresent: true,
expected: true,
},
{
desc: "Knight is awake. Archer is sleeping. Prisoner is awake. Dog is not present",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: true,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight is awake. Archer is sleeping. Prisoner is awake. Dog is present",
knightIsAwake: true,
archerIsAwake: false,
prisonerIsAwake: true,
dogIsPresent: true,
expected: true,
},
{
desc: "Knight and archer are awake. Prisoner is sleeping. Dog is not present",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: false,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight and archer are awake. Prisoner is sleeping. Dog is present",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: false,
dogIsPresent: true,
expected: false,
},
{
desc: "Knight and archer are awake. Prisoner is awake. Dog is not present",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: true,
dogIsPresent: false,
expected: false,
},
{
desc: "Knight and archer are awake. Prisoner is awake. Dog is present",
knightIsAwake: true,
archerIsAwake: true,
prisonerIsAwake: true,
dogIsPresent: true,
expected: false,
},
{
desc: "Everyone is sleeping. Dog is not present",
knightIsAwake: false,
archerIsAwake: false,
prisonerIsAwake: false,
dogIsPresent: false,
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if got := CanFreePrisoner(tc.knightIsAwake, tc.archerIsAwake, tc.prisonerIsAwake, tc.dogIsPresent); got != tc.expected {
t.Errorf("CanFreePrisoner(%v,%v,%v,%v) = %v; want %v", tc.knightIsAwake, tc.archerIsAwake, tc.prisonerIsAwake, tc.dogIsPresent, got, tc.expected)
}
})
}
}
+3
View File
@@ -0,0 +1,3 @@
module annalyn
go 1.26