Comparing wait-signal alternatives for synchronizing two piece of programs

I came across following problem:

A certain computation generates two arrays a and b such that a[i]=f(i) for 0 ≤ i < n and b[i]=g(a[i]) for 0 ≤ i < n. Suppose this computation is decomposed into two concurrent processes X and Y such that X computes the array a and Y computes the array b. The processes employ two binary semaphores R and S, both initialized to zero. The array a is shared by the two processes. The structures of the processes are shown below.

+-------------------------+-------------------------+ | Process X:              | Process Y:              | | private i;              | private i;              | | for (i=0; i < n; i++) { | for (i=0; i < n; i++) { | |    a[i] = f(i);         |    EntryY(R, S);        | |    ExitX(R, S);         |    b[i]=g(a[i]);        | | }                       | }                       | +-------------------------+-------------------------+ 

How to correctly implement ExitX() and EntryY()?

The given solution was:

+---------------+----------------+ | ExitX(R, S) { | EntryY(R, S) { | |   P(S);       |   V(S);        | |   V(R);       |   P(R);        | | }             | }              | +---------------+----------------+ 

where P() is a wait operation and V() is a signal operation.

My doubt is will it make difference if we swap these operations, some like below?

+---------------+----------------+ | ExitX(R, S) { | EntryY(R, S) { | |   V(R);       |   P(R);        | |   P(S);       |   V(S);        | | }             | }              | +---------------+----------------+ 

I believe there is no difference in the behavior and overall outcome of the code when either of above two solutions is used. Am I right?

Getting program states from two different terminals and comparing them

I want to run two different instances of a program in two different terminals. and check the variables inside the program if they match all the time or not.

What I really want to do here is, do observational Determinism in two different programs. I will be calling different functions in both the programs in both the terminal and their respective states.

How can I do so?

Algorithm for comparing object dates and types

I’m looking for a good / efficient algorithm to compare classes of driver licences (or any type of similar objects) and their dates, so that they do not overlap and come in certain order.

eg. We have G1, G2, G, M, etc.

I need to make sure all instances of G1 do not overlap, G1 comes before G2 and does not overlap, and G comes after both, and M is unrelated (there can also be multiple instances of each type, in which case they must not overlap either).

I’m looking to do this without a monster class of if/else statements that no one can ever read.


Class for comparing objects in Python

Im curious how to improve this code. I don’t like creating objects of four different types of reporters, because this could provide some problems with testing. How about the rest of this code, for example the if-statement below?

"""Module containing classes for comparing objects""" import senders import reporters  class Comparator:     """Class for comparing objects"""      def compare(self, compared, competitors):         reporter = reporters.Reporter()         rules = ComparatorExtensions(compared, competitors, reporter)          send_with_email = senders.SendWithEmail()         send_with_sms = senders.SendWithSms()         save_to_file = senders.SendToFile()         show_on_console = senders.ShowOnConsole()          if rules.rule_valid_compared():             if rules.rule_compared_smaller():                 send_with_email.send(reporter.format_report())              if rules.rule_compared_two_times_smaller():                 send_with_sms.send(reporter.format_report())          report = reporter.format_report()         save_to_file.send(report)         show_on_console.send(report)   class ComparatorExtensions:     """Class for extensions for comparator class"""      def __init__(self, compared, competitors, reporter):         self.compared = compared         self.competitors = (item for item in competitors if self.valid_item(item))         self._reporter = reporter      def valid_item(self, item):         result = 'loading_time' in item         if not result:             self._reporter.report_invalid(item)         return result      def rule_compared_smaller(self):         result = False         for item in self.competitors:             self._reporter.report_loading_time(item)             if item.get('loading_time') < self.compared.get('loading_time'):                 self._reporter.report_smaller(self.compared, item)                 result = True          return result      def rule_compared_two_times_smaller(self):         result = False         for item in self.competitors:             if (item.get('loading_time') * 2) <= self.compared.get('loading_time'):                 self._reporter.report_smaller_two_times(self.compared, item)                 result = True          return result      def rule_valid_compared(self):         rule_valid = self.valid_item(self.compared)         if rule_valid:             self._reporter.report_loading_time(self.compared)         return rule_valid 

Full repository:

Comparing objects in a list against each other

I have a list of ~27.000 objects. Each object represent a line from a file where each line is one record of a measurement from some instrument. The most import aspects of the object is:

  • Instrument name: Int (49099, 89,..)
  • Station Name: String (K-900, MK-45,..)
  • Time: Datetime object (13:34:17 01/02/2017)

All these objects are going to be used in creating a .h5 file where the top most “layer” is a measurement. Each measurement contains multiple of these line objects that has the same name and have a difference in time within some duration (30 minutes). One major problem is that the data files I am reading from when creating the line objects are very unstructured. I cannot assume that lines that are after each other in the file have anything to do with each other, so I cannot compare each line to the previous line to have some filtering logic in the reading part. Even files that have been generated on the same date SHOULD be looking similiar, just with different instrument name, but this is not the case for this problem. Thats why I am reading them all in and THEN comparing all lines to each other. But it is taking a very long time and not scalable at all.

The code provided is what I am currently doing and I would love to hear any improvements I could make or different ways to combat my problem.

new = [] for i, r in enumerate(self.records):     x = (y for y in self.records if  y.compare_record_same_name(r))     if any(r in x for x in new):         continue     else:         new.append(x)  class Record():     def compare_record_same_name(self, other):         duration = abs(self.date_time - other.date_time)         duration = duration.total_seconds()         return ( == and duration < TIME_SEPERATOR                 and duration > 0)  ``` 

Iterating over an array comparing input to a String

I have a string array holding words that I am converting to a char array. Also, I am taking users input as a string, then converting it to a char and adding it to an array. I am trying to check user input against the word and if it matches just println correct or incorrect. I am not certain how to code it.

private String[] wordsForGuessing = new String[] {"one", "david",                                           "storage", "unhelpful"}; private String ranWord = randomWord(wordsForGuessing); private char[] convertRanWordToACharArray = ranWord.toCharArray();  private int MAX_SIZE = countsHowManyLettersAreInAWord(ranWord); //adding three extra turns to guess private char[] letter = new char[MAX_SIZE + 3];  private String guess;  //Check user input to see if the letter is in the word private void checkUserInputToRanWord(char[] word, String guess) {     String message = "This is ";      for(int i = 0; i < word.length; i++)     {         if(guess.charAt(0) == word[i])         {             message += " correct guess";         }else{             message += " incorrect guess";         }     }  } 

Panda dataframe Creation a new column by comparing all other row

I have the following example:

def function(value,df):     return len(df[(df['A']<value)])  df= pd.DataFrame(0, index=np.arange(30000), columns=['A']) df['A']=df.index.values  start=time.time() df['B']=pd.Series([len(df[df['A']<value]) for value in df['A']]) end=time.time() print("time:",end-start)  start=time.time() df['B']=df['A'].apply(function,df=df) end=time.time() print("time:",end-start)  start=time.time() series = [] for index, row in df.iterrows():     series.append(len(df[df['A']<row['A']])) df['B'] = series end=time.time() print("time:",end-start) 


time: 19.54859232902527 time: 23.598857402801514 time: 26.441001415252686 

This example create a new column by counting all the row which value is superior to the current value of the row.

For this type of issue (when I created a new column, after comparing for a row all other row of the dataframe), I have tried the apply function,list comprehension and classic loop but I think they are slow.

Is there a faster way?

Ps: A specialized solution for this example is not the thing which interested me the most. I prefer a general solution for this type of issue.

An another example can be: for a dataframe with a columns of string,create a new column by counting for each row the number of string in the dataframe which begin by the string first letter.

Comparing two cells and adding check-mark to matching values

I have two columns, one with all items in a set and another with the items I currently have. I would like to compare those and for the matching results to have a bit of text added if they match. Here is an example.

So in that example, Column A is the set number, Column B is the set values, Column C is what I have currently, and Column D is the expected result.

So I want to compare B to C and if their are names matching, then I want B to have the word ‘CHECK’ appended to the matching name.

I thought about doing this to get the word ‘CHECK’ added


But do I also need a LOOKUP and LEN function here?

Would apps script maybe be easier?

Thanks for any advice,

Comparing cohomology using homotopy fibre

I have a question, which might be very basic, but I don’t know enough topology to answer.

Suppose you have a map of topological spaces (or homotopy types) $ f : X \to Y$ , with homotopy fibre given by $ F$ . We get an induced morphism on cohomology $ f^{*} : H^{*}(Y) \to H^{*}(X)$ . If $ F$ is $ n$ -connected (for all homotopy fibres), can we thereby conclude that $ f^{*}$ is an isomorphism up to degree $ n$ ?

I know that this holds in the case that $ f$ is locally a fibration between manifolds (for example a submersion).

Another case I have in mind is the truncation map $ p : X \to K(\pi_{1}(X),1)$ , which has homotopy fibre given by the universal cover $ \tilde{X}$ of $ X$ . In this case we see that if $ \tilde{X}$ is $ n$ -connected, then $ H^{k}(\pi_{1}(X)) \cong H^{k}(X)$ for $ k \leq n$ , where the left cohomology group refers to group cohomology.

awk – comparing MD5 in two different files

Good day

I have two files Here is the first line of each lets call them A and B


    0075e97d637e643aff05ffcc04cd0701    RAW_DAY_01_190520/A016R374/A016R374_2019-05-20_203047.mhl 


3a70395ca1d499f115095c26b5fc0040  MAINSTREAM/DAY_01_190520/RAW_DAY_01_190520/A013R374/A013R374/A013C001_190520_R374.mxf 

First one A start with 2 spaces before text B start right on text.

I’m trying to build a script that can do the following.

Go line by like of B comparing the Hash to A and append OK to the end of the line of B if it’s found