Implement system to get most traded stocks in C#

Please suggest way to solve a coding question, I came across. Question is as follows:

Implement a class StockMarket that has the two following functions:

 class StockMarket  {   public:  void addTrade(string stockName, int share);  - This function keeps track the number of shares transacted for a stock by adding the trade information  void printTop(int numberOfStock);  - This function prints the top numberOfStock stocks that has the highest number of transacted shares   } 

And here’s a sample calling sequence that uses the above class:

 StockMarket stockMarket = new StockMarket();  stockMarket.addTrade("GOOGLE", 50);  stockMarket.addTrade("APPLE", 150);  stockMarket.addTrade("GOOGLE", 100);  stockMarket.addTrade("MSFT", 250);  stockMarket.addTrade("GOOGLE", 200);   stockMarket.printTop(2); 

Expected output: GOOGLE 350 MSFT 250

I followed 2 approaches to solve this problem:

  1. Create Dictionary with Key – CompanyName and value – number of shares. While giving top results, I sorted dictionary by descending and returned results.

Time Complexity: Add – O(1) | GetTop – O(nlogn) + O(k)

  1. Create SortedSet with custom comparer to sort stocks with descending order. I am facing challanges to update SortedSet. I am not able to remove old entries with time less than O(n).

Please find below my current code :

public class StockNodeComparer : IComparer<StockNode> {     public int Compare(StockNode s1, StockNode s2)     {         int result = s2.value.CompareTo(s1.value);          if (result == 0)         {             return 1;         }         else         {             return result;         }     } } public class StockNode {     public int value;     public string name;     public StockNode(string n, int i)     {         name = n;         value = i;     } }  public class StockContainer {     public Dictionary<string, StockNode> map;     public SortedSet<StockNode> sortedStockList;      public StockContainer()     {         map = new Dictionary<string, StockNode>();         sortedStockList = new SortedSet<StockNode>(new StockNodeComparer());     }      public void AddValue(string s, int x)     {         if(map.ContainsKey(s))         {             int tempVal = map[s].value;             sortedStockList.Remove(map[s]);             var tempNode = new StockNode(s, tempVal + x);             AddNodeInDict(s, ref tempNode);             sortedStockList.Add(tempNode);         }         else         {             var tempNode = new StockNode(s, x);             AddNodeInDict(s, ref tempNode);             sortedStockList.Add(tempNode);         }                 }      private void AddNodeInDict(string s, ref StockNode tempNode)     {         map[s] = tempNode;     }      public void GetTop(int k)     {         int i = 0;         while(i<k)         {             Console.WriteLine(sortedStockList.ElementAt(sortedStockList.Count - i).name);         }     } } 

Are most Linux systems that allow non-root users to execute code straightforwardly rootable?


long story short if you can execute code on a box it is usually straightforward to get root

(quote source)

The immediate implication of this quote (if it’s accurate) is that if you’re running a multi-user system and don’t try your darndest to prevent all users from creating files with x permission set, the system is as good as compromised. The corollary is that operating a multi-user system, such as ones typically found in universities, that by design allow all students to do exercises in C, C++, assembly etc, is pointless, since any student can straightforwardly root this system.

Since running computer systems intended to be used by more people than their owners is not considered pointless, and privilege limiting facilities (users’ rights management, sandboxing, etc etc) are not considered useless, I somehow doubt these kinds of comments. But what do I know?

Is it true that most Linux systems are straightforwardly rootable by anyone who can execute code on them?

Python 2: Most efficient way to find spatial order from a list of tuples

I have a circle-growth algorithm (line-growth with closed links) where new points are added between existing points at each iteration.

The linkage information of each point is stored as a tuple in a list. That list is updated iteratively.

enter image description here

QUESTIONS:

  • What would be the most efficient way to return the spatial order of these points as a list ?

  • Do I need to compute the whole order at each iteration or is there a way to cumulatively insert the new points in a orderly manner into that list ?

enter image description here

All I could come up with is the following:

tuples = [(1, 4), (2, 5), (3, 6), (1, 6), (0, 7), (3, 7), (0, 8), (2, 8), (5, 9), (4, 9)]  starting_tuple = [e for e in tuples if e[0] == 0][0] ## note: 'starting_tuple' could be either (0, 7) or (0, 8), starting direction doesn't matter  order = [starting_tuple[0], starting_tuple[1]] ## order will always start from point 0  idx = tuples.index(starting_tuple) ## index of the starting tuple   def findNext():     global idx     for i, e in enumerate(tuples):         if order[-1] in e and i != idx:             ind = e.index(order[-1])             c = 0 if ind == 1 else 1             order.append(e[c])             idx = tuples.index(e)   for i in range(len(tuples)/2):     findNext()  print order 

It is working but it is neither elegant (non pythonic) nor efficient. It seems to me that a recursive algorithm may be more suitable but unfortunately I don’t know how to implement such solution.

Also, please note that I’m using Python 2 and can only have access to full python packages (no numpy).

This question has also been posted on SO.

What is the most damaging Time Stop combo?

I’m running a powerful lich and I want to nuke the party from orbit when he gets low on HP. The plan is that he will pop Time Stop, and cast a flurry of spells that will all hit the party at once.

Scenario 1: The main stipulations I’m working with are these:

  • Assume Time Stop lasts the full duration.
  • Assume the Lich can cast any spell, 9th level and below, that rolls dice for damage (so no Power Word: Kill), given standard available spell slots.
  • No spells are allowed that cancel the Time Stop early by dealing direct damage to the party – that spell must be used at the last round of Time Stop. (This means Delayed AoE Spells are a go.)
  • Assume the party can all be hit within a 30ft diameter.

Scenario 2:

Imagine the same stipulations as Scenario 1, but Time Stop lasts only 3 rounds.

So far, the best combos I’ve come up with involve casting Time Stop, Delayed Blast Fireball, and some kind of nuke like an upcast Fireball; not that creative but I’m sure there are quite a few combos I’m missing.

Why did most of my Drush commands disappear?

Drush 9.5.2 with Drupal 8.6.3 (updating to 8.6.6 once I get Drush working again). We manage a lot of Drupal 8 sites, but for some reason, Drush stopped working properly on just one of them.

Most commands are now “not defined.” This includes cr (cache rebuild), updb (update database), status, and anything normally listed under core, entity, role, user, etc. When I run “drush list” it only gives me the following categories: _global, config, pm, sql, theme, and webform. The rest have just disappeared.

Everything else about the site seems to work fine. Normally, I would run “drush status” to see if it was seeing the site properly. But just like the other commands, running it just returns a big red “Command ‘status’ is not defined.”

Where should I start looking to figure out how Drush broke and how to fix it? Thanks in advance!

Ensuring Users Get the Most Up-to-Date PDF

I have taken over a Drupal 7 site that contains a lot of pdf forms. These forms are frequently updated. Many of these forms were uploaded to sites/default/files/userfiles and manually linked across the site.

To prevent having to track down each link, when I update a form I have been replacing the file using the same filename. This naturally isn’t a great solution because people are loading the old versions from cache locally.

Is there a simple solution I am overlooking?

Note: I am using the media module for new forms going forward, but wonder if there is a solution for the old files that were done “improperly” as it were.

[ Civic Participation ] Open Question : Among the prospective 2020 presidential candidates I’ve listed, who are you most likely to support and which ones would you approve of?

Joe Biden Michael Bloomberg Richard Blumenthal Cory Booker Sherrod Brown Steve Bullock Peter Buttigieg Julian Castro Mark Cuban Andrew Cuomo John Delaney Jeff Flake Tulsi Gabbard Eric Garcetti Kirsten Gillibrand Kamala Harris John Hickenlooper Eric Holder Jay Inslee Dwayne Johnson Tim Kaine John Kasich Joseph P. Kennedy III Amy Klobuchar Mitch Landrieu Terry McAuliffe Jeff Merkley Seth Moulton Richard Ojeda Martin O’Malley Beto O’Rourke Deval Patrick Tim Ryan Sheryl Sandberg Bernie Sanders Howard Schultz Tom Steyer Eric Swalwell Donald Trump Mark Warner Elizabeth Warren Mark Zuckerberg