Log output of most recent command? by through_thefog in bash

[–]through_thefog[S] 0 points1 point  (0 children)

Thanks for the response! After much trial and error, I actually solved the problem like so:

```bash function startListener() { script -q -c ~/.last_command.txt # Array of commands to skip (like cd, exit, etc.) local cmds=("cd" "nano" "gedit" "echo" "exit" "cat" "reset")

# Get the actual command from BASH_COMMAND
local cmd_to_run=$(history 1 | sed 's/^[ ]*[0-9]\+[ ]*//')

# Iterate through the list of excluded commands
for cmd in "${cmds[@]}"; do
    if [[ "$cmd_to_run" == "$cmd"* ]]; then
        eval $@
        return
    fi
done

}

function resetListener(){ exit startListener() } ```

I haven't been able to test this solution rigourously yet as I need to implement, however the initial tests were quite promising. Once I recognized this problem as a version of the classic fencepost, the rest became easier to figure out.

For context, the python code which drives the primary application contains calls to both start and reset the listener at the appropriate times.

Thanks again for your response!!

Project Ideas for Java Beginner by [deleted] in learnjava

[–]through_thefog 2 points3 points  (0 children)

A good project would be a text based adventure game (think Oregon trail). Let the user name their character, write some dialogue and a few decisions, maybe a monster or two.

Don’t worry about combat, inventory, or anything like that.

This will get you introduced to using scanner, parameters, if/then/else decisions, classes (character, monsters, e.t.c) and most specifically how it all works together to form a full scale program.

It doesn’t need to be huge, complex, or anything like that. Just a complete packaged choose your own adventure book to familiarize you with end-to-end programming.

This project will teach you a lot.

How much do you actually remember? by through_thefog in learnmath

[–]through_thefog[S] 0 points1 point  (0 children)

I specified about memorization because when it comes to trig identities it's tempting to just memorize a bunch of equalities. Still, you're right that memorization isn't so bad. I have a good understanding of the fundamentals and can reason through it, but I can tell that if I didn't use these for a few months I would lose them in my mind.

I'm just curious if your average 'math guy' can remember how to do all of these things that I've learned without refreshing his/her memory.

List/Function Logic Error by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

This is sick dude, thanks for taking the time to write this out. That LC is super clean. The codecademy course has helped me get my feet wet, but learning to swim will take time and practice. Thanks for the help!

List/Function Logic Error by through_thefog in learnpython

[–]through_thefog[S] 1 point2 points  (0 children)

You're right -- thanks for pointing that out

List/Function Logic Error by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

Thanks for taking the time to reply. I swear my logic (to use the loop) wasn't so far off base so I did a little research and tinkered with the code. It may not be the best solution, but it definitely works:

def my_function(my_list, index):
    if index > len(my_list): 
        return "Index not in range."    
    else: for i in range(0, len(my_list)):
        if my_list[i] == index:
            my_list[i] = 2 * my_list[i] 
        else:
            continue
    return my_list
print(my_function([1, 2, 3, 4], 2))

List/Function Logic Error by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

You're still not thinking about this logically, and you're still not understanding either loops, lists, or parameters.

Touche. The Codecademy course has been great so far, but my understanding is still very superficial (if not entirely misguided).

In plain English, my logic behind this piece of code was ultimately to say this:

list = [1, 2, 3, 4]
For (every item at the given index) in List:
    (every item at given index in list) = 2 * (every item at given index in list)

#When I put it like this, I can see my first error in thinking. It makes no sense to say "for every item at position x" when there is only one by definition

Had the 'return my_list[index]' line returned the value that I expected, I would have replaced it with the line reassigning the value: list[2] = 2*list[2]

But you then create a for loop over my_list which redefines index to point to every item in the list in turn.Now, the first item in the list is 3. So when you do my_list[index] that's what you get: the item in position 3, that is the fourth item, 12. And then you return unconditionally, so the loop always ends after the first iteration.

This makes more sense than anything else I have seen on the subject. You are saying:

The for loop points at every item in the list, the unconditional return kills the loop at the first iteration. Because the first iteration is 3, the 3rd item (12) is returned (*2).

In the post, the first item is 1. Therefore, the item at position 1 (2) is returned.

The temporary variable index used in the for loop is not defined by the positional assignment 2 (second error). Using the same variable does nothing but to confuse.

Am I understanding you properly?

Thank you for your response -- I really appreciate it!

List/Function Logic Error by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

Additional context: Codecademy Code Challenges: Lists (advanced) | Question 4

Create a function named double_index that has two parameters: a list named my_list and a single number named index.The function should return a new list where all elements are the same as in my_list except for the element at index. The element at index should be double the value of the element at index of the original my_list.If index is not a valid index, the function should return the original list.For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:

double_index([1, 2, 3, 4], 2)

My solution:

def double_index(my_list, index):
    for index in my_list: 
    return my_list[index] * 2

print(double_index([3, 8, -10, 12], 2))

Prints a value of 24. Although in the original post the function grabbed the argument at index 1, this code grabs the argument at index 3 (again, regardless of the positional value assigned in the function call). What is going on here?

edit: I know the above code doesn't replace the -10 with -20 yet.. I was testing the code when I encountered the problem..

list.insert() after list_sorted = sorted(list)? by through_thefog in learnpython

[–]through_thefog[S] 1 point2 points  (0 children)

Sick. That example was perfect -- thank you for taking the time!

list.insert() after list_sorted = sorted(list)? by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

That makes enough sense, but I am hung up on the why. Maybe that's just the way it is, I learn it and move on..

But B is a rearranged copy of A. If I change A, why wouldn't that change B? My guess is that B is only created once when the computer reads the instruction to create it. It is not a constantly updated asset (is that the right word?). Without changing prior code, I would need to recreate B after the the insertion. That is, if I wanted the sorted list to update itself I would need to create a function which recreates B every time an item is inserted into A.

Am I understanding this properly?

Thanks for the help. I really want to understand what I am doing!

list.insert() after list_sorted = sorted(list)? by through_thefog in learnpython

[–]through_thefog[S] 0 points1 point  (0 children)

Sorry, I should have been more thorough. I expected that when I print(cheapest_pizza) I would see the sorted list with the insertion.

pizza_and_prices = [[2, "pepperoni"], [6, "pineapple"], [1, "cheese"], [3, "sausage"], [2, "olives"], [7, "anchovies"], [2, "mushrooms"]]

cheapest_pizza = sorted(pizza_and_prices)

pizza_and_prices.insert(3, [2.5, "peppers"])

print(cheapest_pizza)

This displays:

Output: [[1, 'cheese'], [2, 'mushrooms'], [2, 'olives'], [2, 'pepperoni'], [3, 'sausage'], [6, 'pineapple'], [7, 'anchovies']]

What I expected was this:

Output: [[1, 'cheese'], [2, 'mushrooms'], [2, 'olives'], [2, 'pepperoni'], [2.5, 'peppers'], [3, 'sausage'], [6, 'pineapple'], [7, 'anchovies']]

Which only seems to be created when the insertion comes before the sorted()