❔ I need help with Binary search alphabetically
Guys, I have this assignment where they want me to implement this Binary search algo
114 Replies
Ok. And?
I recently, learned Binary search for list of integers & strings, but now I'm trying to search alphabetically. My question remains, how do I start or should I covert type int to char or something?
why did you start a new thread and what looks like throw away what you already had? https://discord.com/channels/143867839282020352/1099919832419422218
if you already got it working for 2 data types then it should be easy to do a third
here we go again..
array of strings? then look for first letter of the word that matches the character provided
see
String.StartsWith(Char)
modify your binary search to include thisMy friend, this is another project, it's not belong to the one that I'm building currently. That one is for searching strings, but this one should be searching alphabetically.
dude, binary search is binary search
there is no other binary search
learn the concept
not the task
better yet, try to understand it
binary search is: from a sorted list of items, repeatedly halve it until you find what you are looking for
that's it
i also don't know what the difference is supposed to be between "searching strings" and "searching alphabetically"
you generally compare strings according to some kind of alphabetical order
I do understand the concept and you're right. It's the same pattern for all implementation, I agree with you.
piece of advice
you need to start thinking more
i've been following your posts
Well! You need to ask the course host not me 😆 they're the one who send me the assignment.
and everyone has been helping you
it's time you rise up
and learn for yourself
and the next thing you ask here is in the form of: I did this, and that and that, and also this, and it's not working because of this error, but i think i can solve it by doing this, what do you think?,,,,,,,,,,,,,,,,, instead of here is my assignment, what do i do?
THINK!!!!!!!!!!!!!!!
THINK
THINK
THINK
START THINKING FOR YOURSELF
Ok! I guess when I search on Google and don't find the answer or the issue that I'm solving and when I can't find the answer, this is the last place I would need to reach for the solving the issue.
that's an excuse
i'm not saying don't ask for help
there are tons of times where google won't give you a copy and paste solution
Yeah! That's right.
but you can still take what you find and try to apply it to your problem
you know how i learn?
by failing
over and over and over again
there is no shame in that
evening
i'm currently teaching myself labview for work by going through existing applications with basically no documentation
i don't have google to help me
i just poke at stuff until it breaks, then figure out why it did that
then i learn
I've been there
exactly my breakfast
love it
which is how i learned C# too fwiw
making shitty winforms calculators just to try stuff out
Really! That's very impressive lol.
IMO it's not impressive, it's a standard skill that any programmer needs to have
Yeah! You need to account on yourself and try things out on your own without going to the Google or tutorials directly.
I think Discord is wasting a lot of my time, I would rather go try things on my own or Google things.
from what i've seen you definitely need to spend more time trying to develop a solution yourself before asking for a solution here
see: this thread where you didn't even seem to attempt to solve it
well
I'm not gonna say you shouldn't come here for help
like
don't hesitate to come for help
but fixed is kinda right
you don't show much effort in trying to think through this problem for yourself
and if that's the core problem, then that's what you need to be asking here
it's also a lot easier for people to help you if you can show them something you've tried and then they can give you direction from there
"I need help learning how to logically think through this problem"
cause, yeah, after the past week, that's what you need
you need to learn to think like a computer
it's absolutely a skill, and there's no shame in not being good at it
unless you're not willing to work on it
You guys are giving me a really good advices, I will start a new strategy from now own and develop my solving problem & logical thinking skills. Getting tips from skilled people like you is very valuable and can make a big progress in my learning how to code journey. Thank you
do you want help figuring out this BinarySearch method?
I guess this is what developers do and this is what they get paid for, building softwares by solving problems and write logic. They don't go to Discord so often to ask people or Google all the time, they use their experience & their skills.
yes and no
if you think I don't goolge shit regularly, you're wrong
shit I've done before
shit I do regularly
"Google" is just one of the ubiquitous tools, and yeah, there were programmers before Google
but they didn't do shit blind or from memory
technical jobs thrive on technical documentation
before Google, you'd be regularly referencing your bookshelf of textbooks on various programming languages, machine interfaces, etc.
I have learned the Binary search and how it works, but I'm wondering about like comparing alphabetically.
Google is generally not my "first" stop for something, it's more likely docs.microsoft or MDN or whatever other site has technical documentation for the thing I'm trying to use
comparing what alphabetically?
what do you think that means?
how is the binary search algorithm that you've now "learned" not doing that?
Ok! Very interesting informations. You're right, I wonder how people were programming before without Google or Youtube, some guy told me here, I learned coding where the Discord didn't exist.
comparing alphabetically means following the words in alphabetical order A B C D Adam Bob Conor David
okay
again
you've now written a binary search algorithm, yes?
how is it not doing that?
cause it is
Ok! Good point.
that's what
StringComparer
does
something we've gone over several times now
it compares strings
by character
and you then use those comparisons to perform the searchSo this is what the method is doing
yes
Ok
you are using the instance of
StringComparer
that performs comparisons by ordinal character comparison, I.E. raw byte value comparison
except it also ignores case
and as discussed yesterday, don't, use StringComparer.CurrentCulture
or StringComparer.Ordinal
Also CompareTo() too as they mentioned
and since the byte values of Unicode characters are ordered alphabetically, that gets you alphabetical comparison
ah
okay
so, they want you to use a
.CompareTo()
method, specifically
if that's part of the assignment, then that's part of the assignmentAha! You're right. Otherwise in what order it should compare, like there's nothing else to compare other than alphabetical order.
String.CompareTo Method (System)
Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.
long-story-short, the
string.CompareTo()
method functions exactly as StringComparer.Compare()
it's just a method upon string
, instead of on a separate StringComparer
instance
so
if you were using
that is an easy translation to use .CompareTo()
insteadMan, I'm just jealous of you and the level of knowledge that you're capable of in coding. You're these alphabetical and the Binary machine code zeros, ones 00000110 and the chars and so on.. are mentioned in the ASCI too
practice
Ok! Great explanation, good job man. Then I'll use CompareTo() in my logic.
let's see it
I'll implement the project and show you what I have done
nah, just start with that one line
translate this line to use
.CompareTo()
instead
ask questions if you need guidanceOk! Let me show you.
You're awesome bro, thanks a lot.
Does the ordinal needs to be included or not in this context? String.CompareTo(list<strings> myList, target)
nah, there's no real equivalent
per documentation
Both overloads of the CompareTo method perform culture-sensitive and case-sensitive comparison. You cannot use this method to perform culture-insensitive or ordinal comparisons.
what is culture sensitive means?
not ordinal
I mean, I don't FULLY know
Aha ok
my guess would be like
Ok
if you compare, like, an "a" against an "a" with an accent
ordinally, those are very far apart
like, the "a" with an accent would ordinally come after "b"
does it mean like every culture write things differently or something
it means there are lots of different cultural rules, yes
"current" culture means .NET is going to look at the settings on your PC to figure out what to use
Ok
I got an idea about this topic
I don't think it plays a very big roll
culture?
case sensitive I would say is more relevant particularly in Binary searching
yes
right
What do you think my friend?
well, you're not using
.CompareTo()
on line..... 11?You mean I should've included the middle variable inside the brackets?
on line 11 I'm basically comparing if the element is equal to the middle of our list
right
on line 11 I'm basically comparing if the element is equal to the middle of our listyou didn't use
.CompareTo()
string.CompareTo(Object)
is going to give you pain
and thats what you are doing atmthat too
that's just flat-out wrong
you also have the wrong return type, again.
and you need to use conditionals (if or switch) and adjust your left/right based on the returnvalue from your compare
I mean, it's an in-progress implementation
can we focus on one thing at a time?
Its just that this binary search mess has been going on for over a week at this point.
indeed
there's a certain amount of time-zone mismatch, I think
Not for me, me and Faraj are both based in Sweden.
If it was that easy it wouldn't take a week. It took a week, but the knowledge is permanent.
This seems a good example of what I'm doing currently
sure.
Great
You're comparing them twice. Remove the equals check == and just use CompareTo
Since you have to use an ignore case comparison, forget == exists for strings
What 🙆🏻♂️ I didn't notice that. You're right. So this is why it says you can't convert string to int because middle variable is int.
I have a bug and it's really pain in ass
The random method generates, and the algorithm sorts, but then it says in Swedish the list wasn't sorted, you need to sort first.
It doesn't even checks the dame first if statement
so what's the first step in finding out why it's printing that message?
good question
i'm asking you
I know lol
Well! There are two assumptions here.
I don't really know actually
I thought about some assumptions that could be the issues, but I don't think they're the problem.
Very weird
I'm sure the bug is located in the case 6 in the if conditions
the first step is to use your debugger and set a breakpoint on the line that you think is writing the message
Since the default condition statement
else
is working, that means the problem is in the if statementand verify that your assumption is correct
because just a minute ago you sent two screenshots of two different places in the code that can print that message
This is the problem man, I don't come here and ask for help directly.
But this is my issue and why I keep asking many questions in Discord is that I'm using VS on Mac and the debugger is not very effective as on VS on windows. Look
Yes, but they're the same.
Let me debug and show you
what should i be seeing here? it looks like the breakpoint worked like it should
still not seeing what's wrong with your debugger, that looks fine
so is that mean it's a logical error?
is what a logical error?
i'm just saying your debugger works so i don't know why you're saying it doesn't
I've watched a tutorial on debugging and you can see multiple windows in your ide where you can check all the variables the methods and the values, I don't see the same here 🤷🏻♂️
have you looked at this? https://learn.microsoft.com/en-us/dotnet/core/tutorials/debugging-with-visual-studio-mac
Debug a .NET console application using Visual Studio for Mac - .NET
Learn how to debug a .NET console app using Visual Studio Mac.
I mean the bug surly is not a compiler error or syntax error, so it should be a logical error.
I think I've seen this
all i'm asking you to do is start with the line that prints the message and work backwards to figure out what program state is leading to that message being printed
then you can figure out if something is being done incorrectly
it's not a compiler error, if it was your code wouldn't run
Ok! You're right. I've done that for the last hours and not sure what the problem is honestly.
so what steps did you take and where are you currently at?
have you eliminated any possibilities?
Thank you for asking these questions
First, I add two curly brackets and an default else to the last if statement. But it didn't make any different
second I deleted the key part because if condition doesn't end up with a semicolon and I though this might be the issue
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.