Welcome to Day 2 of the Blind 75 Challenge! Today I will be tackling the problem of finding the maximum profit by buying and selling stock once, a common problem in algorithm interviews and coding competitions. In this blogpost, I will explore a simple and efficient algorithm to solve this problem in Python, using only one pass/iteration through the array of stock prices.

You are given an array `prices`

where `prices[i]`

is the price of a given stock on the `ith`

day.
You want to maximize your profit by choosing a *single day* to buy one stock and choosing a *different day in the future* to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

In this question, we are given an array of stock prices, where each element in the array represents the price in a particular day, and the `ith`

index location of a day of the stock price corresponds the the `ith`

day. We are expected to find a solution that will provide the maximum profit. The maximum profit here is defined as the largest difference between the largest positive number between the selling price and the buying price. As it is we would want to maximize the profit by buying the stock on one day and selling it on a different day in the future.

For example given the array below

```
[1, 2, 3, 7, 4, 3]
```

The maximum profit would be `6`

. Since the minimum price in the array is `1`

and the maximum price is `7`

(which comes at a later day).
Again, the task is to find the solution that gets the maximum profit from the array.

**Naive solution**
One possible approach for finding the maximum profit by buying and selling stock is to first find the minimum and maximum values in the array and then calculate the difference between them. This can be implemented as follows:

```
minimum_price = min(input_list)
maximum_[price = max(input_list)
```

Then get the maximum profit by finding the difference between the maximum price and minimum price.

```
maximum_profit = maximum price -minimum price
```

While finding the minimum and maximum values in the array and subtracting them to get the maximum profit might work in some cases, it is not a correct solution in all cases. This approach is not always correct as it fails to consider cases where buying the stock on a day preceding the selling day would result in a greater profit.

Consider the following example:

```
[3, 2, 6, 5, 0, 3]
```

If we simply find the minimum value (0) and the maximum value (6), we would get a profit of 6 - 0 = 6, which is incorrect. The correct maximum profit that can be made in this case is 6 - 2 = 4, by buying the stock on day 2 (price 2) and selling it on day 3 (price 6). Since you can only buy on a day preceding the selling day. Therefore, finding the minimum and maximum values in the array and subtracting them is not a correct solution for this problem. Instead, we need to use an algorithm that finds the maximum profit that can be made by buying and selling the stock once.

**Using One-Pass Algorithm**

To overcome the limitations of the naive approach, a one-pass algorithm can be used. This algorithm processes each element of the data structure only once and keeps track of the minimum price seen so far and the maximum profit that can be made from selling the stock at the current price.

Here are the steps for implementing the one-pass algorithm:

- First check if the list is empty. If empty return 0 as maximum profit.
`if not prices: return 0`

Initialize the minimum price to the first element in the array.

`maximum_profit = 0`

Traverse through the array.

`for price in input_list:`

Check if the current price is lower than the minimum price.

`if price < minimum_price:`

If it is, update the minimum price (since no profit can be made from a lower price.

`minimum_price = price`

Else calculate the profit that can be made by selling the stock at the current price. This is the difference between the current price and the minimum price so far.

`else: profit = price - minimum_price`

Finally, compare the current profit with the maximum profit seen so far and update the profit if the current profit is greater.

`if profit > maximum_profit_seen: maximum_profit_seen = profit`

Return the maximum profit obtained. ```python return maximum_profit_seen

Using this algorithm, we can find the maximum profit that can be made by buying and selling the stock once, taking into account the constraint that the buying day must precede the selling day.

```
def maximum_profit_buy(input_list: list):
# Check if the input list is empty
if len (input_list) == 0:
return 0
# Initialize the minimum price and maximum profit seen so far
minimum_price = input_list[0]
maximum_profit_seen = 0
# Traverse through the input list
for price in input_list:
# Update the minimum price seen so far
if price < minimum_price:
minimum_price = price
else:
# Calculate the profit that can be made by selling at the current price
profit = price - minimum_price
# Update the maximum profit seen so far if the current profit is greater
if profit > maximum_profit_seen:
maximum_profit_seen = profit
# Return the maximum profit seen so far
return maximum_profit_seen
```

Let's test the `maximum_profit_buy`

function:

```
print(maximum_profit_buy([7,6,4,3,1])) # Expected output: 0
print (maximum_profit_buy ([1, 2, 3, 7, 4, 3])) # Expected Output 6
```

The First test case represents the array `[7,6,4,3,1]`

, where the stock price decreases every day. In this case, no profit can be made, so the expected output is `0`

. For the second test case, we have an array `[1, 2, 3, 7, 4, 3]`

and the maximum profit that can be made by buying stock on `day 1`

`price 1`

is and selling it on `day 4`

`price 7`

is 6 which is the expected output.

The function has a time complexity of O(n), where n is the length if the input array. This is so since we need to iterate through the array only once. The space complexity is O(1) since we only use a constant amount of extra space to store the minimum price seen so far and the maximum profit.

The problem of finding the maximum profit by buying and selling a stock once is a common problem in coding interviews and competitions. It can also be used in finance and economics to analyze the performance of stocks and investments.

In this blog post, we explored a simple and efficient algorithm to solve the problem of finding the maximum profit that can be made by buying and selling a stock once. By using the one-pass approach and keeping track of the minimum price seen so far and the maximum profit that can be made by selling the stock at the current price, we can solve this problem in O(n) time complexity, where n is the length of the input array.

]]>Given an array of integers `num`

and an integer `target`

, return indices of the two numbers such that they add up to `target`

.
You may assume that each input would have exactly one solution and you may not use the same element twice.
You can assume that the given input array is not sorted.

The two-sum problem as it is widely called is a classic coding challenge that requires finding two integers in a given list that add up to a target value. The problem is often presented in different technical contexts, for example in algorithmic design, data structures, and optimization or even in the form of interview questions for most software engineering positions.

Now, to the main thing, this problem requires us to find two integers provided they are present in the given list that add up to a given target value. So for example if given `example_list`

and a `target_int`

below:

```
example_list = [2, 3, 6, 9]
target_int = 9
```

You would be expected to come up with a code that returns the index location of `3`

and `6`

since these are the integers that add up to the target integer, such that your return value is:
`[1, 2]`

**layman's thought**

When I first approached the Two-Sum problem, my initial thought was to find a way to map each number in the input list to its corresponding index location. I realized that this could be achieved by creating a table or dictionary that stores each number as a key and its corresponding index as the value. Such that for the list below:

```
example_list = [2, 3, 6, 9]
```

you would have a table similar to the one below:

Elements | Index Location |

2 | 0 |

3 | 1 |

6 | 2 |

9 | 3 |

Next, I iterated over the input list and for each number, I calculated the difference between that number and the target integer. I then checked if this difference exists in the input list (excluding the current number being checked). If the difference was found in the list, I used the table or dictionary I created earlier to find the index location of the number that makes up the target sum. This gave me the indices of the two numbers that add up to the target value.

In summary, my solution involved creating a table or dictionary that maps each number to its corresponding index location in the input list, and then iterating over the list to find the difference between each number and the target integer. I then used the table or dictionary to find the location of the number that makes up the target sum.

**Pythonic thoughts**

The table can be presented in Python as a hash table or dictionary data structure that maps each integer in the input list to its corresponding index location. This will enable us to access the index location of any integer in constant time. To do this, I created a dictionary variable that will store the integers as keys and index location as values. In Python the index location and elements can be gotten using the `enumerate`

method. This will return both the index location and element while iterating through a list:

```
cache = {el: en for en, el in enumerate(input_list)}
```

Next, iterate over the input list and for each integer, calculate the difference between that integer and the target(given):

```
for en, int_1 in enumerate(input_list):
```

Next, check if the difference exists in the input list (excluding the current integer being checked). This is achieved by looking it up in the dictionary. This search operation takes constant time.

```
if (target_int - int_1) in input_list :
if cache[target_int - int_1] != en:
```

If this search operation is successful and the difference is found in the input list, use the dictionary to look up the index location of the integer that makes up the sum.

```
return [cache[int_1], cache[target_int-int_1]]
```

If no match is found, i.e. no two integers add up to the target value, we return an empty list.

```
return []
```

```
def two_sum(input_list: list, target_int:int):
# Create a hash table or dictionary that maps each integer to its index location
cache = {el:en for en, el in enumerate(input_list)}
# Iterate over the input list and check for the sum of two integers that equals the target value
for en, int_1 in enumerate(input_list):
if (target_int - int_1) in input_list:
# Check that the two integers are not the same
if cache[target_int - int_1] != en:
# Return the indices of the two integers that add up to the target value
return [cache[int_1], cache[target_int-int_1]]
# Return an empty list if no two integers add up to the target value
return []
```

To test if the code works:

```
# Example usage
list_1 = [2, 3, 6, 9]
print(two_sum(list_1, 9)) # Output: [1, 2]
```

The time complexity of this solution is O(n), where n is the length of the input list, and the space complexity is also O(n) since we need to store each integer in the input list as a key in the dictionary.

The two-sum problem is a common problem in computer science and is used in many real-world applications. For example, in financial applications, we can use the two-sum problem to find pairs of stocks that add up to a given target value. In image processing, we can use the two-sum problem to find pairs of pixels that add up to a given target colour.

In this blog post, we discussed the two-sum problem, the intuition behind solving it, and how to solve it using a dictionary/hash table. We saw that this problem has a time complexity of O(n) and a space complexity of O(n). We also discussed some use cases of the two-sum problem in real-world applications.

]]>2. "**Mao: The Unknown Story**" by Jung Chang - A historical exploration of Mao Zedong's life and impact on China.

3. "**The Irish Difference: A Tumultuous History of Irish breakup with Britain**" by Fergal Tobin - An in-depth examination of Irish culture, including its historical background and unique characteristics.

4. "**Multiple View Geometry in Computer Vision**" by Richard Hartley and Andrew Zisserman - I have read papers from both authors, fascinated by their works.

5. "**Bayesian Reasoning and Machine Learning**" by David Barber - the future is plagued with uncertainty and so is our physical world. Building an interactive machine for our physical world requires understanding uncertainties and mitigating their ripple effects. An exploration of the integration of Bayesian reasoning and machine learning for modeling uncertain systems and mitigating their potential impact.

For the last few months, I have been bothered by the ideology peddled by most employers of labour. This very concern has led me to ask a not-so-popular question am I being approached for employment or opportunities because of the colour of my skin? Perhaps, this question is popular howbeit in the minds of the most concerned few. This rather daunting question, even led to a more stomach-turning one is this equity or equality If my question turns out to be true? Of course, if False, am I being headhunted because of my intelligence, skills, and diversity of my uniqueness? Or is it rather because of prejudice? If True, does this mean I am privileged and profiting from an undeserved opportunity?

As a researcher, when I am faced with a challenging technical problem, especially the ones that leave me tasking for days, I am led to examine the base class. In object-oriented programming, a base class is a fundamental template or blueprint on which other classes are built. These newly created classes inherit functionalities, methods, and principles from the base class. It is also to be noted that new methods, principles, and ideology can be created which can override the inherited methods. Please hold this thought as this will make more sense soon. Back to my original ponder, the questions I have asked myself for weeks have led me to this one question. Which is best Equality or Equity? Or succinctly put Which is the more noble, just, and fair goal- Equity or Equality? While both have inherited the ideas of social justice, fairness of rights and opportunities, one more than the other, is overriding the very fundamental truth of the base class while claiming to belong to the base class.

As a society, we constantly debate over whether equality or equity is the more desirable goal. On the surface, the two concepts may appear to be interchangeable, but upon deeper examination, it becomes clear that they represent fundamentally different ways of thinking about the world.

Equality is the absolute ideal that everyone should be treated equally, regardless of background or characteristics. This is a noble goal and one that is deeply ingrained in our culture. The idea that all people should be treated with dignity and respect is a fundamental principle of democracy. However, the problem with this approach is that it assumes that everyone starts from the same place and that the same opportunities are available to everyone.

This is a fallacy. In fact, there is no one equal person and in truth, people have different starting points and challenges to overcome. Some individuals may have had a privileged upbringing, while others may have struggled with poverty or discrimination. Treating everyone the same, without taking these differences into account, can perpetuate inequality, defeating the main purpose of fairness, diversity, and inclusion.

Equity, however, is the idea that everyone should have and be provided/presented with an equal opportunity to succeed. This means that individuals and groups who have been traditionally marginalized may require additional resources or support to achieve the same level of success as those who have not faced such barriers.

To achieve equity, we must be willing to acknowledge and address the ways in which structural inequalities exist in our society. This requires us to take a step back and examine the systems and institutions that shape our lives. We must ask ourselves: Are the playing field and opportunities equal for all individuals? Are certain groups or individuals facing barriers or discrimination that make it harder for them to succeed? It is only by acknowledging these difficult truths and taking steps to address them, that we can truly achieve a society that is fair and just for all. Equality may be a nice idea, but it is not enough. We must strive for equity if we are to create a society in which everyone can reach their full potential.

It is however important to note that achieving equity does not mean that everyone will have the same outcome, but rather that everyone will have the same opportunity to succeed. This means that some individuals may still achieve more success than others, but it will not be due to systemic barriers or discrimination that has constantly plagued our society. More importantly, equity is not about granting preferential treatment to certain groups or individuals, but rather about levelling the playing field and providing the necessary resources and support to overcome barriers.

Additionally, equity must be seen as an ongoing-continuous process, as society is ever-changing and dynamic- opportunities to address inequalities, challenges, and discrimination will always arise. In practice, achieving equity may involve a variety of actions, such as implementing policies and practices that promote diversity and inclusion, creating more accessible educational and job training programs, and addressing biases in hiring and promotion practices.

Ultimately, the goal of equity is to create a society in which everyone can reach their full potential, regardless of their background or characteristics. It's not only morally right but also beneficial for society, as a diverse and inclusive society is more productive and innovative.

In conclusion, while equality is a very noble goal, it is not enough to achieve a truly just and fair society. Equity is the more desirable goal, as it acknowledges and addresses the structural inequalities that exist in our society and ensures that everyone has an equal opportunity to succeed. This requires us to be willing to look beyond the surface and examine the systems and institutions that shape our lives. Only by achieving equity can we create a society in which everyone can thrive.

]]>Over the last couple of months, I realised that I have had to write the same codes all over again and manually do some tasks that I could have easily automated. These usually lead to a lot of boilerplate codes. Being the curious mind that I am- I decided to write a package that I could easily install on my PC and run these tasks whenever I so desire. I can also plug this package as part of a larger project code base.

So, here is introducing `[Ormedian-Utils](https://pypi.org/project/ormedian-utils/#description)`

a python package for basic CV tasks.

As of now `ormedian-utils`

can do the following

- Save frames from videos, camera feed or a folder containing more than one video.
- move specific files from myriads of other files.
- Resize images in folders or multiple folders.

Read the docs here .

I hope you find the package useful.

]]>But this quote has stayed with me for the last 3 months.

`If you ever feel like a failure, remember failure is part of succeeding`

This has kept me going. I hope whatever you are facing now or will face in the future you will have the strength to pull through and come to the realisation that it's all part of the process. 9th, August 2022.

]]> I am currently working on Self-Learn-Your-Key Gaze (*SLYKGaze*), A Gaze Estimation Technique that minimizes domain expertise limitation and aleatoric uncertainties in learning-based gaze estimation.

I will be joining Belfast Metropolitan College in September 2022 as a Part-Time Lecturer in Machine Learning, and Part-Time Lecturer in Python.

In July 2022, our ethics application was accepted, hence we are set to collect the first-of-its-kind dataset.

Status | Milestone | Goals | ETA |

ðŸš€ | H-E-O | 0 / 1 | March 2022 |

ðŸš€ | SLYKGaze | 5 / 10 | `in progress` |

ðŸš€ | HRI Dataset | 4/ 10 | `in progress` |

ðŸš€ | RDSH - A deep neural network for pose estimation with densenet as the backbone. collaborative work with [Federico Zocco] | 1 / 3 | `ongoing` |