Difference between binary search and binary sort [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What is the difference between binary search and binary sort? (Or is there no such thing as binary sort.)
Can I perform binary search on an unsorted list? (I am a little in clear on this can someone please explain it to me.)
If I perform binary sort on an unsorted list and then binary search for an element in that (now) sorted list what will be the time complexity of the whole process?

Can I perform binary search on an unsorted list?(I am a little in clear on this can someone please explain it to me)
No you cannot,
The core idea of Binary search is to reduce the search space by value comparison.
for instance look at [1,2,3,4,5,6,7] and the element to be search for is 5.
in binary search we look at the middle element in this case 4, since the target element is greater than 4 and we know the array is sorted we can look only at the right half [5,6,7]
Binary search can also be implemented where the function is MONOTONOUS , example
[T , T , T , T , F , F , F]
You can apply binary search to find the first False in the array with this template
start=0, end=size
while(start<end)
if(condition)
end=mid
else
start=mid+1
return start
returning start will give you the first element which is false.

Related

Averaging the digits of pi in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
As a preface, I must admit that this is my first real attempt to code a simple program from scratch, and I've just begun my foray into learning python.
As a simple project, I wanted to write a program to find the average value of the digits of π. In other words, we all know that π=3.141529....., but I set out to determine the value of (3+1+4+1+5+....)/(# of digits summed).
This was the result:
#retrieve the value of pi
n=math.pi
#request the number of digits of pi to average over
mag=input("how many terms?")
mag=int(mag)-1
#generate a string populated with the whole number values of the digits of py
k=int(round(n*10**(mag),0))
digits=[int(d) for d in str(k)]
print(k)
#sum the specified values of the digits of pi
print(sum(digits))
#recall the length of the string
print(len(digits))
#calculate average
print((sum(digits)/len(digits)))
The code runs well enough, but I am curious about what tweaks I could make to improve the program or simplify it.
Most specifically, I would like to know if there there is a simpler or more direct way to cast the individual digits of pi into a list or string of integers for manipulation. For instance, is there a specific operator one could use to call individual digits of a given number, like digit(0) returns 3 with respect to pi.
Any advice is greatly appreciated!

Ranking preference of each item in a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've been away from Python for awhile, please forgive the broad/basic question.
Let's say I have a list of items, which can be anything really.
I want to write a program that prints two of these at a time, in every possible combination of two, and lets the user choose a preference/winner. Then at the end print the full list with some sort of numerical preference value for each. I don't know enough about the mathematics of rank or preference to know what that number would even look like...
Basically I'm having writer's block right from the start here. Any advice on how to structure this or what I should be looking into?
A very simple way of doing it would look something like this:
initialize a scoring_list
for every element in a list
for every element in a list
while valid input:
ask the question via raw_input()
if input matches the first item
store winner in some sort of scoring list
tell program input is valid
else if input matches the second item
store winner in some sort of scoring list
tell program input is valid
else
tell program input is not valid and repeat question
do some math with the scoring_list (e.g. normalization)
print( scoring_list )
Obviously a lot of detail is left out intentionally, because it depends on programming style and objectives. For example, it may be better to do the loops via indices so that you can have a scoring list with indices that matches your original list. With this structure, you can be creative with how you are tracking the "ranks" with something more statistically rigorous than a straight count.

Indexed-based numbering in pseudocode [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm studying a-level computer science and my text shows array pseudocode declarations, some starting at 1 and others 0. Can anyone tell me why this is the case. Please note that I am studying Python.
DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list
DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list
DECLARE List3 : ARRAY[1:100] OF INTEGER // 100 elements in this list
DECLARE List4 : ARRAY[0:25] OF STRING // 26 elements in this list
There are languages that use either or both, and some algorithms are easier to express with one or the other. For instance, a textbook heap uses 1-based indexing. C and Python use 0 based indexing, Pascal and Ada let you choose, Lua and Matlab use 1 based. In practical terms, you mostly need to be aware which is used in the language you write.
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) lists the conventions used in a few languages. One bit of trivia that's not in there is the C way to find the number of elements in an array: sizeof(array)/sizeof(array[0]). It's rarely used because C's calling conventions strip size information by demoting arrays to pointers, anyway.
In languages like for example Pascal you actually decide what is the lower and upper bound for indexing. This is the notation the text is using.
In Python and most other programming languages instead the first element is always at index 0 and when declaring an array you only say what is the number N of elements.
In a few badly designed programming languages the first element is instead at index 1.

How to represent "range" in python dict [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to create dictionary like this:
1,2 = "A"
3,4 = "B"
5,6,7・・・(>=5) = "C"
so I tried below, but didnt works well..
dict = {1:"A", 2:"A", 3:"B", 4:"B", >=5:"C"}
how can I create a dictionary like this?
A lookup of a value where the key is in a sorted range is capped at O(log n). Hence using a dictionary directly is not viable. You can either do this by:
storing the ranges in list, sorting them, and doing a binary search, or...
using a tree data structure.
There are other creative solutions such as storing all the numbers in the range in the dictionary as integer keys and have duplicate references to the same value (which looks like what you have above). This would be O(1) lookup, but would have the negative side effects such as:
cannot modify the ranges easily without updating all the keys
wasted space for large ranges
cannot do lookups with floats
...which might be okay depending on your use case.
In summary, there is no direct solution.

Read each line in a text file, store the values in a list, and compute scores [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Read each line in a text file, store the values in a list, and compute scores
The best approach this problem is to list all the functions you need to do the task. A typical example is:
Read file.
Read list.
Take list and get each string delimited by space.
Store string into an array.
...
Then go to the Python website and lookup how to do each function.
Example: To do input and output function in python:
https://docs.python.org/2/tutorial/inputoutput.html
Also, you can look up function by asking google. Google will then point you to answers to your questions:
Q: How to find mean of a list?
A: Finding the average of a list
If you do this enough times, eventually you will be able to write a problem that solves the problem listed.
Good Luck!

Categories