• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Does it get easier?

I constantly worry if this is just the bare basics, and that further classes/learning will go right over my head. I am improving with Python, but I can't tell if it's normal progression or slow going.

There are people in class who say they've done programming for a living and claiming the professor isn't explaining things well. I do feel he needs to dumb down things more, and he also creates these sudden rule shifts that throw me out of a loop (for instance, he claimed he couldn't open my email with the last assignment because he got a virus warning, so I have to deliver it directly in class).

Well, sure it gets easier in the sense that it took me about half a second to figure out your problem. I guessed what it was before I even looked at the code :)

Programming is as much experience as it is studying. You can read 100 books about something, but there's no substitute for getting your hands dirty. That's why there's people who are probably in your class aceing everything. It's not that they're smarter, it's that they've probably been tinkering with programming and computers since they were kids.

I'm not saying that's necessary in order to be successful, the point is just that it takes time. It's not for people who want an easy pass through college. But then again, the fields that pay well never are. If something was easy and it paid well and didn't require luck to be successful, everyone would do it, right?

In any event, I need to take that code and have it search via binary algorithm. I'll start searching/trying it myself, but if anyone immediately knows how to do that it would save me time.

I think this would be doing you a disservice, and as such I hope nobody else answers your question for you. Not trying to be rude, but you said you wanted it to get easier. But as I've already explained, there's no substitute for experience, practice, and discovering on your own. So if you want it to get easier, you need to struggle through it. That doesn't mean people can't help, but certainly just telling you how to do everything without you first struggling a little on your is not going to get you any closer to this stuff being easier for you.
 

SOLDIER

Member
Wasn't asking that someone just edits my existing code, just the proper coding string where I could then try applying it myself.
 

Slavik81

Member
Wasn't asking that someone just edits my existing code, just the proper coding string where I could then try applying it myself.
When I want to understand some algorithm, to apply it to my code, the very first place I check is Wikipedia. It generally has good descriptions.

Though, I was generously given Donald Knuth's TAoCP as a gift. Maybe in the future, I should check it before checking Wikipedia. I'm ashamed to admit, I might not actually read much of the book otherwise.
 

Koren

Member
Current problem is that it will say that the number was not found on the list, even if I type in a number that should validate. Help, as usual, would be appreciated.
Just a suggestion for this kind of debugging...

When you write something that exists in Python (which happens all the time when learning), you can always try using the Python function besides your own.

By simply displaying the boolean result of
Code:
search_value in numbers
the 'false' you'll get will show you that there's at least an error *outside* of your search function.


Beware with things you find online, btw, input works differently in Python 2, and you wouldn't encounter the bug. Be sure to check whether it's Python or Python3k examples.
 
Wasn't asking that someone just edits my existing code, just the proper coding string where I could then try applying it myself.

Right, I'm just saying that part of the learning process involves figuring that out yourself too. If you come and show some code that says "Here's what I've written, but it doesn't work because of X, Y, and Z" then we can provide some guidelines. But for "I need to implement a binary search, what should I do", all I can say is "try to write a binary search". Look up a description of the algorithm, and try to make some code that does that.
 

SOLDIER

Member
Right, I'm just saying that part of the learning process involves figuring that out yourself too. If you come and show some code that says "Here's what I've written, but it doesn't work because of X, Y, and Z" then we can provide some guidelines. But for "I need to implement a binary search, what should I do", all I can say is "try to write a binary search". Look up a description of the algorithm, and try to make some code that does that.

Understood.

Here's the code for binary search provided in the companion book:

Code:
def binary_search(arr, value):
    first = 0
    last = len(arr) - 1
    position = -1
    found = False

    while not found and first <= last:
        middle = (first + last) / 2

        if arr[middle] ==value:
            found = True
            position = middle

        elif arr[middle] > value:
            last = middle - 2

        else:
            first = middle + 1
        return position

I need to figure out how to apply that to my existing (and working) input validation code:

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")
    
main()

This part I could really use help with.
 
I glossed over this, but I'm not sure exactly what to look for. How does clang's modules impl deal with header-only classes/templates/etc?

I'm not the one implementing modules and i only partially follow what's going on so take this with a grain of salt, but my understanding is thatat:

A) with modules enabled, #defines are not allowed in header files , so the compiler is guaranteed that the header looks the same every time it's included. (I think this is where msvc and clang differ, btw)

B) the compiler essentially solves the infamous "export problem " (which you can read about in Herb Sutters paper on why export should be removed)

With this in mind, you can think of modules as doing the same thing as precompiled headers. It spits out more or less an AST of the headers, so that if anyone tries to include it, it doesn't have to parse the header or do anything, it's all already parsed and an AST ready to go for the entire module.
 

cocopuffs

Banned
Welcome to every Introduction to Programming course ever. It's not your professor, it's that it's hard.
On Assignment 2 of my Intro Programming Course (CSC108) and this shit is kinda hard. What's worse is all those people who've already programmed before talking about how said assignment or said test was so easy. Makes me feel like shit when I'm just struggling to keep up. But I'll manage...I hope.
 

Rur0ni

Member
On Assignment 2 of my Intro Programming Course (CSC108) and this shit is kinda hard. What's worse is all those people who've already programmed before talking about how said assignment or said test was so easy. Makes me feel like shit when I'm just struggling to keep up. But I'll manage...I hope.
This happens frequently. Don't sweat it. Once you wrap your head around it (flow control, functions, data structures), you should be in good shape.
 
I'm not the one implementing modules and i only partially follow what's going on so take this with a grain of salt, but my understanding is thatat:

A) with modules enabled, #defines are not allowed in header files , so the compiler is guaranteed that the header looks the same every time it's included. (I think this is where msvc and clang differ, btw)

B) the compiler essentially solves the infamous "export problem " (which you can read about in Herb Sutters paper on why export should be removed)

With this in mind, you can think of modules as doing the same thing as precompiled headers. It spits out more or less an AST of the headers, so that if anyone tries to include it, it doesn't have to parse the header or do anything, it's all already parsed and an AST ready to go for the entire module.
Ok, this sounds nice.

Standardizing it can't happen fast enough, IMO
 
This part I could really use help with.

Good news, you're ninety percent of the way there.

Think of your program as a set of Lego blocks. Each task being accomplished in your program is one Lego block. However, just like Legos each section connects to the next by connecting them together. The binary search code you've been given is a single Lego block because it accomplishes one task. Is there a part of your program that accomplishes the same task as the binary search code?
 

SOLDIER

Member
Good news, you're ninety percent of the way there.

Think of your program as a set of Lego blocks. Each task being accomplished in your program is one Lego block. However, just like Legos each section connects to the next by connecting them together. The binary search code you've been given is a single Lego block because it accomplishes one task. Is there a part of your program that accomplishes the same task as the binary search code?

I figure it's a simple manner of combining them, but I currently don't see a way to make the "pieces" fit.

I'm sure I'll smack my head at the final solution, but please provide another hint, or sample code.

The good news is that once I get these assignments working, I can go back to them for reference for future projects.
 

Makai

Member
One major annoyance of mixing F# and Unity is you cannot ignore the output of a function called in Update(). Unity crashes if you try.
 

OceanBlue

Member
I figure it's a simple manner of combining them, but I currently don't see a way to make the "pieces" fit.

I'm sure I'll smack my head at the final solution, but please provide another hint, or sample code.

The good news is that once I get these assignments working, I can go back to them for reference for future projects.

So I'm inexperienced in programming compared to most people on this thread, but to me it seems to me that this exercise is trying to get you to think about how to break down problems into tasks. IMO, you should just make a list of instructions for completing the task. Don't worry about how to express it in code or about being too specific about how your program is going to do something. You should just try and tell a story about what you want to do. For example:
  1. Get a list of numbers
  2. Get a number to search for
  3. Search for the number in the list
  4. Report whether you found the number or not
Once you have that, look at your code and try to see if you can break your code into steps to help you solve your solution. Does it matter how you do any of those tasks? If getting the number you wanted to search for was hard-coded, i.e.
Code:
    search_value = 5552012
what would that change?
 

peakish

Member
I figure it's a simple manner of combining them, but I currently don't see a way to make the "pieces" fit.

I'm sure I'll smack my head at the final solution, but please provide another hint, or sample code.

The good news is that once I get these assignments working, I can go back to them for reference for future projects.
So as I understand, you basically have to replace your own search with the provided binary search function? I'd start with converting your own method to a function that works in the same way:
Code:
def my_search(arr, value):
    index = 0
    [...]
    return index
You then only need to call the function with your numbers array and search value as inputs, and catch the returned index. Once that works, you can replace the function call with the provided binary_search function and it should work, since the function signature is identical. Edit: The binary search function returns -1 if the value wasn't found, so your function should also do that.

However, I think there's a bug in the binary_search function. I believe that this part should be
Code:
elif arr[middle] > value:
    last = middle - 1 # Not middle - 2
and that you need to convert the middle index to an integer after calculating it.

Also, as I gather the binary search method only works on sorted arrays. The one in your current code is not sorted so it'll give bad results.
 

Koren

Member
However, I think there's a bug in the binary_search function. I believe that this part should be
Code:
elif arr[middle] > value:
    last = middle - 1 # Not middle - 2
and that you need to convert the middle index to an integer after calculating it.

Also, as I gather the binary search method only works on sorted arrays. The one in your current code is not sorted so it'll give bad results.
You're right. I suspect it's Python code, though, not Python 3k, so the division isn't really a bug.

You definitively should not convert to an int, though, but use integer division // You won't have large enough tables to have issues, and half integers are free of rounding errors, but it's still not great to use floats there...


There's also at least an indentation bug too, for the return.


Fun facts: given *several hours*, only 10% of Bell labs professional programmers managed to write a bugless binary search code, when they did the experiment. I find that scary, especially when my students usually can do it.

Knuth also said that it took 16 years (!!) after the algorithm was published to see the first bugless implementation. It's on the verge of unbelievable...
 

SOLDIER

Member
This is probably nowhere near how it should be, but this is my current attempt:

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

def binary_search(numbers, value):
    first = 0
    last = len(numbers) - 1
    position = -1
    found = False

    binary_search = int(input ("Enter a number to search for in the list: "))

    while not found and first <= last:
        middle= (first + last) / 2
        print ("That number was not found in the list.")

        if numbers[middle] ==value:
            found = True
            position = middle

        elif numbers[middle] > value:
            last = middle -1

        else:
            first = middle + 1
        return position

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1
        print ("The number was found in element " + str(index + 1))
    
main()

No errors listed, but no prompt to put in the value either. The way this needs to work is that I am prompted to enter a value, and the code says whether it's part of the number list or not.
 

Koren

Member
I won't give you all the things to correct, but some suggestions to study:
- you define a search function, how is it called?
- what does the first while? The second one?
- does your list satisfy the requirements for a binary search?
- when the execution reach a return, the function returns immediately. When does it happen here?
- is the search value passed as an argument, or read from keyboard inside the function?


Also, check the errors suggested above, and triple-check the indentation...

On the whole, I think you try too early to code. You should have a clear idea of each step involved before even writing a single line. It's a bad habit that virtually all my students have. The current program doesn't make much sense.

You'll progress quickly if you train. But you should be able to follow the execution line by line. Try doing it loud, as if you were explaining it to someone, it can help. It's barely different from a cooking recipe, really.
 

peakish

Member
This is probably nowhere near how it should be, but this is my current attempt:

No errors listed, but no prompt to put in the value either. The way this needs to work is that I am prompted to enter a value, and the code says whether it's part of the number list or not.
You seem to be a bit confused about how to use functions. I'd suggest looking through your textbook and some examples to get a feel for them.

Here's a simplified showcase of what happens in your program: https://repl.it/BUqp
To enter the function binary_search you need to call it: https://repl.it/BUqp/1
A dead simple way to find out what's happening in your code is to print out texts about where you are, so that you can follow what's happening and where things start to go wrong.
 
Ok, this sounds nice.

Standardizing it can't happen fast enough, IMO

So I was slightly wrong about macros. You can use macros in header files, but definitions can only come out of a module, not go into a module. What this means is that if you do something like this:

Code:
#import <module1>
#import <module2>

Then module2 cannot be affected by anything that module1 has done with the preprocessor. You can though. So if you write this instead:

Code:
#import <module1>
#import <module2>
const int x = SOME_DEFINE;

Then as long as module1 or module2 exported SOME_DEFINE, this will work.


Everything else I said was correct, but I should add a little more. When you write the import statement of a module, you aren't actually importing a "header file". You're importing a module map. A module map is a specification that has a different grammar than C++. Think of it like an IDL file if you ever did COM programming. You can put any crazy stuff you want in a header file, but the only stuff exported from the module are things that are called out in the module map. This means you can have some crazy header only class with 1000 methods, but you can only export the stuff that is actually important and that you want the user to see.
 

SOLDIER

Member
A lot of the hints given would make a bit more sense to me if I understood the terminology. It's still slow going for me, and the way I tend to learn is testing the stuff out myself and seeing how it comes together (as has been suggested).

Anyway, this code turns out a result. Not sure it's the correct one, but the project is due today and I would really like to get it working proper. As usual, the help here is greatly appreciated (and I'm sure as hell learning more here than I am in class).

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")

def binary_search(numbers, value):

    first = 0
    last = len(numbers) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position

main()
 
A lot of the hints given would make a bit more sense to me if I understood the terminology. It's still slow going for me, and the way I tend to learn is testing the stuff out myself and seeing how it comes together (as has been suggested).

Anyway, this code turns out a result. Not sure it's the correct one, but the project is due today and I would really like to get it working proper. As usual, the help here is greatly appreciated (and I'm sure as hell learning more here than I am in class).

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")

def binary_search(numbers, value):

    first = 0
    last = len(numbers) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position

main()

You're not actually using the binary search anywhere. You've written the function (or at least, a binary search function appears in this code, but it's not clear if you wrote it or the teacher gave it to you). But you don't actually call it anywhere. It's essentially dead code, you could delete it and the program would be identical because you need to use the function somewhere.
 

SOLDIER

Member
That's pretty much my big problem: I don't know the order of where it needs to go.

I have the binary code, including a printed sample from my professor. I just can't find any instructions on where it goes on the code.

Here's the binary search code by itself, as given by the professor:

Code:
def binary_search(arr, value):
    first = 0
    last = len(arr) - 1
    position = -1
    found = False

    while not found and first <= last:
        middle = (first + last) / 2

        if arr[middle] ==value:
            found = True
            position = middle

        elif arr[middle] > value:
            last = middle - 1

        else:
            first = middle + 1
        return position

Am I supposed to change "value" or "arr" to "numbers", as the number list is called in my input validation code?
 

peakish

Member
That's pretty much my big problem: I don't know the order of where it needs to go.

I have the binary code, including a printed sample from my professor. I just can't find any instructions on where it goes on the code.

Here's the binary search code by itself, as given by the professor:
Am I supposed to change "value" or "arr" to "numbers", as the number list is called in my input validation code?
In a sense. You can call a function with your input array numbers and value search_value. Just match the order of the parameters:
Code:
binary_search(numbers, search_value)

Example: https://repl.it/BUqp/4
 

SOLDIER

Member
Yeah, those are the examples I have found, and I do understand how those work.

What I can't figure out is how to make it so that it prompts me to put in a number value and validate it with the number list in the code. These binary examples already have a number put in to test it; I believe for this assignment I have to have it ask me to put in a number after it's running, not before.
 
That's pretty much my big problem: I don't know the order of where it needs to go.

I have the binary code, including a printed sample from my professor. I just can't find any instructions on where it goes on the code.

Here's the binary search code by itself, as given by the professor:

Code:
def binary_search(arr, value):
    first = 0
    last = len(arr) - 1
    position = -1
    found = False

    while not found and first <= last:
        middle = (first + last) / 2

        if arr[middle] ==value:
            found = True
            position = middle

        elif arr[middle] > value:
            last = middle - 1

        else:
            first = middle + 1
        return position

Am I supposed to change "value" or "arr" to "numbers", as the number list is called in my input validation code?

You should probably review the section in your textbook about what functions are and how to call them.

You don't need to touch that function. (Btw, was that given to you or did you find it online)?

You've already called a number of functions in your program. int(), input(), print(), and len() are all functions which are called in your program. You need to call binary_search somewhere.
 

peakish

Member
Yeah, those are the examples I have found, and I do understand how those work.

What I can't figure out is how to make it so that it prompts me to put in a number value and validate it with the number list in the code. These binary examples already have a number put in to test it; I believe for this assignment I have to have it ask me to put in a number after it's running, not before.
Not really, the binary_search examples all require an input list of numbers and the value that you get when the program starts to run. So you'll have to call the function with those two as parameters, instead of the loop that you're using now in the program. Just replace that entire while loop with the function call.
 

Koren

Member
You're close to the solution.

The 'main' function defines an array (a list) of values, ask for a value from the user and name it search_value, perform the search, and display the outcome.

You DON'T want this function to make the search, since you have a function for that, binary_search.

So you should remove the while loop in main, and replace it with a call to binary search.

Now for the call. Just a reminder about functions. Let's take the pow function that compute x to the power of y. Something like
Code:
def pow(x, y) : return x**y

x and y have no special meaning. It just take the first element in the parenthesis and put it at the power of the other.

pow(2, 3) gives 8

Code:
a = 5
b = 2
z = pow(a, b)
Means that z is linked to the value 25.

So you need to write something like
Code:
xxxxx = binary_search(yyyyy, zzzzz)
Where xxxxx is a name that will contain the result, yyyyy a sorted list and zzzzz the value that should be checked.

About the result returned (which you store in xxxxx), it's the position in the list if the value is in the list, but if it's not, can you see what you'll get as a result?

(I'm not overly fond of this choice, in fact, but that's not you who choosed it)

And, again, beware... You can do a binary search in a *sorted* list!
 

SOLDIER

Member
You should probably review the section in your textbook about what functions are and how to call them.

You don't need to touch that function. (Btw, was that given to you or did you find it online)?

You've already called a number of functions in your program. int(), input(), print(), and len() are all functions which are called in your program. You need to call binary_search somewhere.

The textbook is kind of its own thing and doesn't list much with Python. It won't really help here. The binary key was given by the professor.

I'm sure it's staring me right in the face, but I'm out of ideas, and also time. Could someone please show me how it's supposed to look?

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")

def binary_search(arr, value):

    first = 0
    last = len(arr) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position

main()

I know it should be something like this, I just haven't been able to figure out how to get the whole thing working.
 

arit

Member
The textbook is kind of its own thing and doesn't list much with Python. It won't really help here. The binary key was given by the professor.

I'm sure it's staring me right in the face, but I'm out of ideas, and also time. Could someone please show me how it's supposed to look?

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")

def binary_search(arr, value):

    first = 0
    last = len(arr) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position

main()

I know it should be something like this, I just haven't been able to figure out how to get the whole thing working.

Concentrate on the code in main function, locate the parts of it which searches for a number within an array and its position in it, then think about what binary search does and how it could be used to solve this part.
 
The textbook is kind of its own thing and doesn't list much with Python. It won't really help here. The binary key was given by the professor.

I'm sure it's staring me right in the face, but I'm out of ideas, and also time. Could someone please show me how it's supposed to look?

Sorry, I don't really want to just give you the answer :-/ I know that sucks and it's not what you want to hear, but grades are supposed to be assigned based on how well you understand the material. I know there's probably a lot of legitimate reasons why you might still not understand the material, such as a poor textbook or a poor instructor, but at the end of the day, whatever grade you get should be the one you earned, and someone else telling you the answer isn't earning it :-/

If I were helping you in person I could lead you toward the answer through a more one-on-one type of itneraction, but that's not really possible with this asynchronous communication format. But then again, that's why TA's usually have office hours, or most universities have some sort of help labs where you can go and get help from experienced students and/or TAs. Next time you should consider using one of those for help (if they exist, they may not at your school although they do at all the schools I'm familiar with)
 
I'm on Macbook Pro 2015 El Capitan and I want to get started with Browserify. I'm on the tutorial and I can't run this command "browserify main.js -o bundle.js"

because browserify: command not found

Please help!
 

X05

Upside, inside out he's livin la vida loca, He'll push and pull you down, livin la vida loca
The textbook is kind of its own thing and doesn't list much with Python. It won't really help here. The binary key was given by the professor.

I'm sure it's staring me right in the face, but I'm out of ideas, and also time. Could someone please show me how it's supposed to look?

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")

def binary_search(arr, value):

    first = 0
    last = len(arr) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position

main()

I know it should be something like this, I just haven't been able to figure out how to get the whole thing working.
Well it's not working because:
1 - you are not calling the binary_search method, ever.
2 - position = True should be position = middle
3 - your numbers list isn't ordered

Seems to me like you have an issue with understanding how things are executed in Python because as it stands your code is only executing the "main()" method and nothing else (you *really* should reread the class notes or textbook on methods and how they are called and defined)
 

MiszMasz

Member
Yes, I entered this command:

npm install -g browserify

I'm not on OSX so i can't quickly check exactly where it installs, but it looks like it's not in your path. When you try to run a command like 'browsify -options stuff' without specifying exactly where the executable is on your computer each time, your system basically checks an environment-wide 'PATH' to see if a relevant executable or directory containing one matches there.

Try:
Code:
echo $PATH
See if anything ending in /npm/bin/ is included in that output. If not, try cd'ing to /usr/local/share/npm/bin/ and see if your npm installed stuff is in there.
If (and only if) it looks like the right place, try adding this line:
Code:
export PATH=$PATH:/usr/local/share/npm/bin/
At the end of your .bash_profile file in your home directory. Restart your terminal.
 

reckless

Member
The textbook is kind of its own thing and doesn't list much with Python. It won't really help here. The binary key was given by the professor.

I'm sure it's staring me right in the face, but I'm out of ideas, and also time. Could someone please show me how it's supposed to look?

Code:
def main():

    numbers = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
               8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
               1005231, 6545231, 3852085, 7576651, 7881200, 4581002]

    found = False

    index = 0

    search_value = int(input ("Enter a number to search for in the list: "))

    

    if found:
        print ("The number was found in element " + str(index + 1))
    else:
        print ("That number was not found in the list.")



main()

I know it should be something like this, I just haven't been able to figure out how to get the whole thing working.
You really need to lookup how functions are called and executed

1)You have to call the binary search function, you aren't calling it right now so
Code:
def binary_search(arr, value):

    first = 0
    last = len(arr) - 1
    position = -1
    found = False
    middle = 0

    while not found and first <= last:
        middle = (first + last) // 2
        if numbers[middle] == value:
            found == True
            position = True
        elif numbers[middle] > value:
            last = middle - 1
        else:
            first = middle + 1

    return position
Is doing nothing right now.

2)This section of code is just doing a regular sequential search and is not needed since the binary search function should be used instead
Code:
while found == False and index < len(numbers):
        if numbers[index] == search_value:
            found = True
        else:
            index = index + 1



3) For a binary search to work the array has to be sorted, so element 0 is smallest and the last element is the largest. Your array is unsorted right now.

Just telling you the answer would hurt you a lot more than help you right now.
 
I'm not on OSX so i can't quickly check exactly where it installs, but it looks like it's not in your path. When you try to run a command like 'browsify -options stuff' without specifying exactly where the executable is on your computer each time, your system basically checks an environment-wide 'PATH' to see if a relevant executable or directory containing one matches there.

Try:
Code:
echo $PATH
See if anything ending in /npm/bin/ is included in that output. If not, try cd'ing to /usr/local/share/npm/bin/ and see if your npm installed stuff is in there.
If (and only if) it looks like the right place, try adding this line:
Code:
export PATH=$PATH:/usr/local/share/npm/bin/
At the end of your .bash_profile file in your home directory. Restart your terminal.

Ends in usr/local/git/bin

This PATH business is annoying. :(

I still get yo: command not found after
inserting your export PATH code.

echo $PATH now ends with usr/local/share/npm/bin/
 

Fishlake

Member
Does anyone know if there is a worked through example for AES similar to Orlin's article The DES Algorithm Illustrated?

It would help greatly for debugging my AES code.
 

Martal

Neo Member
Hi guys - wanted to ask. Is this an appropriate place for beginner programming questions? Im going through ThinkPython2 and Im having a couple of issues.
 

JesseZao

Member
Been learning Entity Framework Code First for asp.net mvc at my internship. I have a feeling that it will be hard to go back to database first design. I'll be exclusively creating web apps for our intranet, so I don't know how much anyone will need to have database access outside of the app ui.

My boss seems pretty adamant about a robust database accessed through sql server, but it seems like I can make a good structure by designating join tables manually.

Anyone else use EF?
 

Somnid

Member
Been learning Entity Framework Code First for asp.net mvc at my internship. I have a feeling that it will be hard to go back to database first design. I'll be exclusively creating web apps for our intranet, so I don't know how much anyone will need to have database access outside of the app ui.

My boss seems pretty adamant about a robust database accessed through sql server, but it seems like I can make a good structure by designating join tables manually.

Anyone else use EF?

EF code first is great for quick throwaway type apps. Not really recommended for high volume or performance critical stuff that needs lots of tuning. It's for when the database is more of an irritation than something you want to think about (which is much of the time).
 

JesseZao

Member
EF code first is great for quick throwaway type apps. Not really recommended for high volume or performance critical stuff that needs lots of tuning. It's for when the database is more of an irritation than something you want to think about (which is much of the time).

I don't have a problem creating a database first (it's how I've always done it). I like the "magic" that EF uses because it makes sense and the interaction with viewmodels seems really useful.

I'll look into EF with database first. It just seemed like I could setup my entities well enough to be similar to how I'd design the database manually.

So far, the apps I'm going to make aren't very complicated. Mostly they will track reports and action items for people from the various manufacturing departments to use.
 

Two Words

Member
I thought this was a fun idea for practicing recursion. I wonder if anybody else would come up with the same solution as myself.

Create a recursive function that takes a string, and whatever else you want it to take, and tells you whether or not the string can be constructed with the periodic table's representation of elements. For example, "because" can be spelled "BeCAuSe" or "BeCaUSe". The function should print the string as constructed from using the periodic table like my example. You are allowed to repeatedly use the same element in the periodic table. For example, "HHHHHH" is fine since you can just use six "H" from Hydrogen. Case sensitivity does not matter, obviously. I came up with a solution that gives you all possible combinations to make that string, but I don't know if it is easier if you restrict yourself to just finding one combination.
 
Top Bottom