#C# Annalyns Infiltration Exercise

118 messages Β· Page 1 of 1 (latest)

surreal loom
#

If you look at the variables (since the are either boolean - either true or false) you can invert their meaning if they are prefixed by a !.
So like !archerAwake is read as not archerIsAwake which basically means archerSleeps transforming a if(!archerIsAwake... to if(archerSleeps....

Look at the description of the instructions:

Signal prisoner: the prisoner can be signalled 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.

edit: I just noticed you can archieve the solution with just one single IF block πŸ™‚

polar rain
#

okay u see in my code i do the "!"-technique

#

thats alright but i dont know what i do wrong

surreal loom
#

I don't want to give you the answer right away so I describe the approach I took:
Read the excerpt I quoted in my previous post, watch the parts that are written in bold and underscored. This should give you a hint πŸ™‚

#

you just have to arrange the words a bit differently

polar rain
#

yea i know thats ok but wait i show u the failure picture:

#

is it not like that:

if(!archerIsAwake && !prisonerIsAwake)
 {
            return false;
 
}
#

cannot means return false i guess

#

and there is: if archer AND (&&) prisoner are both sleeping

#

so there are not awake

#

then it must be false

surreal loom
#

As a bit of background knowledge:

AND operator &&

means that both (left and right side) must be evaluted to true so that the result is also true
so

  • false && false -> false
  • false && true -> false
  • true && false -> false
  • true && true -> true
    (side track but in c# if the left side is already false then the right side won't be touched)

OR operator ||

means either side has to be true so the result can be true

  • false || false -> false
  • false || true -> true
  • true || false -> true
  • true || true -> true
#

in this particular case (task #3 in the exercise you refer to) can be solved with just one single IF block. you don't actually need to cover all the other permutations (of awakeness and sleepness of both individuals - the guard and the prisoner)

#

if it helps, the IF block bascially does a boolean operation on what is inside the ()after the if and whatever comes out of that either the // do this (if result is true) or the // do that (if result is true) part will be executed

        result = !prisonerIsAwake && archerIsAwake; 
        if (result){
            // do this    
        }
        else {
            // do that
        }```
polar rain
#

i know yea.. but i dont get the exercise

surreal loom
#

The task is to fill the function CanSignalPrisoner with some sort of logic, that (if called with the 4 different combination/permutations of true and false) it will return in a way that returns a boolean value which matches the expected behavious. So like if prisoner is asleep and archer does not sleep the result has to be false (test #12)

#
!archerIsAwake, !prisonerIsAwake
!archerIsAwake, prisonerIsAwake
archerIsAwake, !prisonerIsAwake

those are the different combinations
only one of them matches the exact requirements mentioned in the instructions for which the function should return true;. find the right combination, return the proper boolean value/true false for which the function "works" and in all other cases (or just by default) return false;

polar rain
#

oh

#

it is so easy

#

normally

surreal loom
#

if you "know it" it's always easy πŸ˜‰

polar rain
#

!archerIsAwake && prisisawake

#

wtf i did

surreal loom
#

you did a || instead of an &&

polar rain
#

ohhh

#

yea

surreal loom
#

which evaluates to true if either side is true

polar rain
#

sometimes the lil things

surreal loom
#

but in this case you have to have both sides be true

polar rain
#

damn

surreal loom
#

tbf it took me a while to find the "feature" in your code too πŸ˜‰

polar rain
#

ahh damn haha

surreal loom
#

then i saw you don't even need the fluff below the first IF check

polar rain
#

tysm

surreal loom
#

yw

polar rain
#

it could be i need mor help in future haha

#

should i close this one here

surreal loom
#

#1082698079163134073 is your place to be pepecute

polar rain
#

sehe auch gerade du kommst aus ger lol

surreal loom
#

tbf idk if closing the threat is the same as just leaving it

polar rain
#

what about the last one

surreal loom
#

?

strong finchBOT
#

If everything is resolved, we ask that the person who posted the request react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!

polar rain
#

same struggle at the last task

surreal loom
#

Ok so I tried around a bit and it seems like if it is not explicitly mentioned that someone has to be asleep or awake you don't need to check for that. Only if their state is mentioned then you have to check for it.
The instructions are layed out in a nested way. The first decision is to check wether or not the dog is present and for each of the two cases you have subdecisions to make. In case dog is present you have to decide wether or not you can rescue the person. If the dog is not present you have to choose wether or not it is possible based on the state of the guards and since the instruction explicitly mentions the state of the prisoner you have a third level of nesting of IF which has to decide if the person can be rescued based on if they are awake or not.

#

It helps to identify the parts of the instruction that actually matter and ignore the text fluff around it πŸ™‚

gilded halo
polar rain
#

Yea sorry guys I will tell you tomorrow. I came home and the workers on the street cut my Ethernet πŸ™‚β€β†”οΈ

#

Have to fix that first…

modest lantern
#

πŸ‘€

polar rain
#

good morning πŸ™‚

surreal loom
#

gm

#

u got your internet back yesterday?

polar rain
#

no sadly not.. its struggeling with the provider (Telekom)

#

they called them tomorrow and now they must find the one who installed the cables.. but now im at work and i could practice here

#

ok so i still stuck at task 4

polar rain
#
 public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
    {
       if(petDogIsPresent && (knightIsAwake && !archerIsAwake) && prisonerIsAwake) {
           return true;
       } else {
           return false;
       }
    }
#

yea i did

#

but it still doesnt work haha

surreal loom
#

if dog is not present your function always returns false. which is not the exepected outcome. if dog is not present you can still rescue the prisoner

#

The instructions are layed out in a nested way. The **first **decision is to check wether or not the dog is present and for each of the two cases you have subdecisions to make. In case dog is present you have to decide wether or not you can rescue the person. If the dog is not present you have to choose wether or not it is possible based on the state of the guards and since the instruction explicitly mentions the state of the prisoner you have a third level of nesting of IF which has to decide if the person can be rescued based on if they are awake or not.

polar rain
#

ohhh

#

ok yea so first time i check if dog is present

#

i tried to do all in one if

surreal loom
#

you can do it in one if. you just need an else part too πŸ˜‰

#

(checking if (dog_is_present) {...} else {...} )

gilded halo
#

Some people have written whole programms in one line. Doesn't mean it's the best way to do things.
Using multiple lines/statements and later on condense them is a reasonable and more beginner friendly approach

polar rain
#
    public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
    {
       if(petDogIsPresent) {
           if(!archerIsAwake && knightIsAwake){
               return true;
           }
       } else {
           if(prisonerIsAwake && !knightIsAwake && !archerIsAwake){
               return true;
           }
       }
    }
#

dont find the issue

surreal loom
#

almost there. in the first check (after the dog check) the state of the knight is not relevant for the outcome of the success for the rescue mission
the second check (the else part for the dog question) the state of the prisoner determines the outcome of the success depending on the state of both guards combined

#

the error message you see means that you have to have a return value after the if {} else {} block IF you have a return type other than void in c#

#

the code has code paths (e.g. petDogIsPresent = true and archerIsAwake = true) that "run through" and never come to a return statement, therefore violating the "every code patch has to return a value" rule of c#

polar rain
#

returned a false after else block

#

now it works

surreal loom
#

you got 4 βœ… ?

polar rain
#

no just to are false...

#

two*

surreal loom
polar rain
#

just these two haha

#

got it

#
public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
    {
       if(petDogIsPresent)
       {
           if(!archerIsAwake && knightIsAwake || !archerIsAwake && !knightIsAwake)
           {
               return true;
           }
       }
        else 
       {
           if(prisonerIsAwake && !knightIsAwake && !archerIsAwake)
           {
               return true;
           }
       }
        return false;
    }
surreal loom
#

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.
this is the first part of the twofold instruction

The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape
aka "the knight is irrelvant for the first chekc

polar rain
#

thats the final...

#

i can submit tysm !

surreal loom
#

yw and congratz!

polar rain
#

im feeling so stupid oh gosh haha

surreal loom
#

don't πŸ™‚

polar rain
#

scared for the next exercise

surreal loom
#

which one?

polar rain
#

i guess now ..strings, i mean i already had the basics more then one time at other pages or sometimes in school, i created wpfs with xaml for UI and code behind c# but now i do this basic stuff here and i kinda despair

#

its so sad

#

i dont know why

surreal loom
#

a quote a heared yesterday
"Athletes still have to do the basics do be come great at their sports"

#

smth like that
πŸ˜‚

polar rain
#

combined lists with datagrids... mvvm pattern design all this stuff but here the easiest exercises makes me sad haha

polar rain
#

but true yea

#

ok i tryna start the next one

#

cant imagine that im sitting at work and have literally no work that i do learning lessons haha

gilded halo
#

I would recommend requesting Mentoring on the site so someone can look at it more and give you tips on how to improve this solution, In Discord the first worry is to actually solve it on the site we go deeper

#

But congrats on solving it (sry I was a bit busy elsewhere would have loved to help)

polar rain
surreal loom
#

If Saburo is talking about the code review, you have to click on the Code Review button after submitting your solution or in the exercise overview after selecting a specific exertices go to the Code Review tab

gilded halo
#

I am talking about code review yes

#

Can take a while tho C# track is really populated I mentored there before so I might see it

strong finchBOT
#

If everything is resolved, we ask that the person who posted the request react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!

polar rain
#

i dont find it on page @gilded halo

#

can u show me where it is?

modest lantern
#

To the left of "Open in editor"

polar rain
#

aight ty

#

i keep grinding in exercises rn

gilded halo
polar rain
#

I think in few min it will come haha I don’t understand extension methods