1

So I have this function and my goal is to print out which portfolios have open positions. A portfolio is represented by a name and a collection of positions. Each position has a name, a start date, and an end date. A position is considered to be open on a given date if its starting date is before or on the given date and its end date is either not specified or on a later date than the given date.

This is the template code.

import sys
from typing import List, Set, Optional
from datetime import datetime

DATE_FORMAT = '%Y-%m-%d'



def get_portfolios_with_open_positions(date_str: str, portfolio_strings: List[str]) -> List[str]:
    # Access your code here. Feel free to create other methods as required
    pass



date_str = sys.stdin.readline().rstrip("\n")
portfolio_strings = []

for line in sys.stdin:
    print(line)
    portfolio_strings.append(line.rstrip("\n"))
    
portfolios = get_portfolios_with_open_positions(date_str, portfolio_strings)

for portfolio in portfolios:
        print(portfolio)

For a given output:

2020-01-01
Portfolio1|CUSIP1:2019-01-01:2022-02-01
Portfolio2|CUSIP2:2019-01-01:2022-02-01

This should be the given input

Expected Output:
Portfolio1
Portfolio2

I am having difficulties in extrapolating the two dates from the portfolio_string, so far I can only extrapolate the first date.

2
  • You need datetime.strptime(DATE_FORMAT,date_string) to parse your dates into objects, which can be compared: docs.python.org/3/library/… Commented Oct 19, 2021 at 21:02
  • @AdamSmooch sorry Adam, I didn't get that part, I apologize, i am new to datetime so I am a little lost. Commented Oct 19, 2021 at 21:13

1 Answer 1

1

Here is my possible answer

def get_portfolios_with_open_positions(date_str: str, portfolio_strings: List[str]) -> List[str]:
    # list for the return value
    output_list = []

    for portfolio in portfolio_strings:
        # splits the portfolio string into the 3 parts that are separated by ':'
        port_full_name,open_date_str,close_date_str = portfolio.split(':')

        
        if datetime.strptime(date_str,DATE_FORMAT) >= datetime.strptime(open_date_str,DATE_FORMAT):
            if not close_date_str or datetime.strptime(close_date_str,DATE_FORMAT) > datetime.strptime(date_str,DATE_FORMAT):
                port_name = port_full_name.split('|')[0]
                output_list.append(port_name)

    return output_list
  1. The function loops through all the portfolio strings in the input list
  2. The function splits the portfolio strings based on the ":" into the three variables (assuming the format of the input will always be the same, if it isn't then there could be a possible error here)
  3. The first if statement compares the dates by using the datetime.strptime function to turn the strings into datetime objects. The function takes two arguments, first is the date string, the second is the date format.
  4. The first checks if there is a value in the close_date_str variable from the split statement. If it wasn't set then it will evaluate to true, or if it was set and the close date is later than the given date it will evaluate to true.
  5. If the last if statement evaluated to true, it gets the base portfolio name from the port_full_name variable, made from the split function, using another split on the "|" and adds the name to the list
  6. After all the portfolio strings have been looped through and checked it returns the output list

Let me know what you think.

Sign up to request clarification or add additional context in comments.

4 Comments

oh thanks, it seems to work😩😩I am starting to think this is the wrong field for me, is there a way I can learn more about this? because I could understand the problem but I just could not translate it into code, I have never used this api before lol.
You're welcome. Coding just takes time like anything else that you would want to learn. To me its a lot like a puzzle, you can have all these different pieces but you may not know how to put them together yet. When you say learn more about this are you referring to coding in general or specifically things about the python datetime library?
coding challenges in particular, i think my brain is not trained enough to understand them quickly.
I'm not sure if you're stuck on basic coding or just specific subjects, but a website that I have used a lot if w3schools.com. They have all sorts of coding tutorials for basic and some intermediate subjects that are free to use and were very helpful to me when I was learning to code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.