Different next states for same current state? - state-machine

So I'm working on this state machine which is supposed to emulate a sequence as follows:
state machine table
However, the state 00 goes to both 01 and 10 at different times. How can I design a circuit that allows me to go from the same initial state to two different next states?

Based on the table you shared, I'm assuming that this machine wants to iterate in the loop 00->01->00->10 an again. In this case, your machine can be modeled using 4 states instead of just 3. These states are:
A represents 00 but when the previous state was 10
B represents 01
C represents 00 but when the previous state was 01
D represents 10
With A as the initial state, your table will then be:
S
S'
A
B
B
C
C
D
D
A
If you want to use the original values, then you must build a circuit that for each of these new states gives you those values. This circuit would be in this case:
S
V
A
00
B
01
C
00
D
10

Related

Rank groups without duplicates [duplicate]

I am trying to get a unique rank value (e.g. {1, 2, 3, 4} from a subgroup in my data. SUMPRODUCT will produce ties{1, 1, 3, 4}, I am trying to add the COUNTIFS to the end to adjust the duplicate rank away.
subgroup
col B col M rank
LMN 01 1
XYZ 02
XYZ 02
ABC 03
ABC 01
XYZ 01
LMN 02 3
ABC 01
LMN 03 4
LMN 03 4 'should be 5
ABC 02
XYZ 02
LMN 01 1 'should be 2
So far, I've come up with this.
=SUMPRODUCT(($B$2:$B$38705=B2)*(M2>$M$2:$M$38705))+countifs(B2:B38705=B2,M2:M38705=M2)
What have I done wrong here?
The good news is that you can throw away the SUMPRODUCT function and replace it with a pair of COUNTIFS functions. The COUNTIFS can use full column references without detriment and is vastly more efficient than the SUMPRODUCT even with the SUMPRODUCT cell ranges limited to the extents of the data.
In N2 as a standard function,
=COUNTIFS(B:B, B2,M:M, "<"&M2)+COUNTIFS(B$2:B2, B2, M$2:M2, M2)
Fill down as necessary.
      
  Filtered Results
        
Solution basing on OP
Studying your post demanding to post any alternatives, I got interested in a solution based on your original approach via the SUMPRODUCT function.
IMO this could show the right way for the sake of the art:
Applied method
Get
a) all current ids with a group value lower or equal to the current value
MINUS
b) the number of current ids with the identical group value starting count from the current row
PLUS
c) the increment of 1
Formula example, e.g. in cell N5:
=SUMPRODUCT(($B$2:$B$38705=$B5)*($M$2:$M$38705<=$M5))-COUNTIFS($B5:$B$38705,$B5,$M5:$M$38705,$M5)+1
P.S.
Of course, I agree with you preferring the above posted solution, too :+)

Groupby column value and keep row based on another column value

I have a DataFrame, df, that has a range of values such as:
ID
Code
01
AB001
02
AB002
02
BC123
02
CD576
03
AB444
03
CD332
04
BC434
04
CD894
I want to remove all duplicates in the ID column and keep the row that has a certain value in Code. Let's suppose that if the Code that starts with BC is available, I want to keep that row. Otherwise, I want to take the first row with the ID. My desired output would look like:
ID
Code
01
AB001
02
BC123
03
AB444
04
BC434
I want to do something like:
# 'x' denotes a list of rows per unique ID
def keep_row(x):
# determine if 'BC' is even an available Code
if any([True for row in x if row['Code'].startswith('BC') else False]):
return the row that has Code that starts with BC
else:
# return the first row with the unique ID if there is no Code that begins with BC
return x[0]
df.groupby('ID', group_keys=False).apply(lambda x: keep_row(x))
I'd appreciate any help - thanks.
You can sort your dataframe by ID and boolean value (False when Code starts with "BC"), then .groupby() and take first item:
df["tmp"] = ~df.Code.str.startswith("BC")
df = df.sort_values(by=["ID", "tmp"])
print(df.groupby("ID", as_index=False)["Code"].first())
Prints:
ID Code
0 1 AB001
1 2 BC123
2 3 AB444
3 4 BC434

how to combine different categorical attribute values in dataframe

I am working on NYC property sales dataset(https://www.kaggle.com/new-york-city/nyc-property-sales).
There is one column "BUILDING CLASS CATEGORY", which has several different categorical string values. What I want to do is to choose only the top 4 categories which have maximum occurrences and combine the rest of the values into a single one.
For ex-
> dataset["BUILDING CLASS CATEGORY"].value_counts()
01 ONE FAMILY DWELLINGS 12686
10 COOPS - ELEVATOR APARTMENTS 11518
02 TWO FAMILY DWELLINGS 9844
13 CONDOS - ELEVATOR APARTMENTS 7965
09 COOPS - WALKUP APARTMENTS 2504
03 THREE FAMILY DWELLINGS 2318
07 RENTALS - WALKUP APARTMENTS 1743
so what I want is that all the instances of top 4 categories are replaced by some integer values like
01 ONE FAMILY DWELLINGS instances are replaced by 0
10 COOPS - ELEVATOR APARTMENTS instances are replaced by 1
02 TWO FAMILY DWELLINGS instances are replaced by 2
13 CONDOS - ELEVATOR APARTMENTS instances are replaced by 3
all the other instances are replaced by integer 4
So next time when I run the command it should output something like this:
> dataset["BUILDING CLASS CATEGORY"].value_counts()
0 12686
1 11518
2 9844
3 7965
4 6565 #sum of all the other instances
I have tried using LabelEncoder but my method is getting too long, so if there is an efficient way to do this, please do tell me.
Let's just call your series like this for short:
building_cat = dataset["BUILDING CLASS CATEGORY"]
This is what you already did:
vc = building_cat.value_counts()
Now get list of top 4:
top4 = vc[:4].index.tolist()
And map it to your df:
building_cat = building_cat.map(lambda x: top4.index(x) if x in top4 else 4)
I didn't download the dataset, if it doesn't work I'll try locally.
You can change type if needed:
building_cat = building_cat.astype("category")

How to extract 5 number and nanmes from the list [duplicate]

I am trying to get a unique rank value (e.g. {1, 2, 3, 4} from a subgroup in my data. SUMPRODUCT will produce ties{1, 1, 3, 4}, I am trying to add the COUNTIFS to the end to adjust the duplicate rank away.
subgroup
col B col M rank
LMN 01 1
XYZ 02
XYZ 02
ABC 03
ABC 01
XYZ 01
LMN 02 3
ABC 01
LMN 03 4
LMN 03 4 'should be 5
ABC 02
XYZ 02
LMN 01 1 'should be 2
So far, I've come up with this.
=SUMPRODUCT(($B$2:$B$38705=B2)*(M2>$M$2:$M$38705))+countifs(B2:B38705=B2,M2:M38705=M2)
What have I done wrong here?
The good news is that you can throw away the SUMPRODUCT function and replace it with a pair of COUNTIFS functions. The COUNTIFS can use full column references without detriment and is vastly more efficient than the SUMPRODUCT even with the SUMPRODUCT cell ranges limited to the extents of the data.
In N2 as a standard function,
=COUNTIFS(B:B, B2,M:M, "<"&M2)+COUNTIFS(B$2:B2, B2, M$2:M2, M2)
Fill down as necessary.
      
  Filtered Results
        
Solution basing on OP
Studying your post demanding to post any alternatives, I got interested in a solution based on your original approach via the SUMPRODUCT function.
IMO this could show the right way for the sake of the art:
Applied method
Get
a) all current ids with a group value lower or equal to the current value
MINUS
b) the number of current ids with the identical group value starting count from the current row
PLUS
c) the increment of 1
Formula example, e.g. in cell N5:
=SUMPRODUCT(($B$2:$B$38705=$B5)*($M$2:$M$38705<=$M5))-COUNTIFS($B5:$B$38705,$B5,$M5:$M$38705,$M5)+1
P.S.
Of course, I agree with you preferring the above posted solution, too :+)

Hexa decimal interpretation

I am trying to connect a pedometer watch to my phone with bluetooth and want to read the steps from it to an app I have made. The connection is made successfully and I am able to read the data from the watch but I am not so clear how to interpret it.
Below is the document,
Eigenvalue content:
(1) all the eigenvalue content inside the endian order are small endian order.
(2) current_pedometer_measurement
The value of the current_pedometer_measurement consists of four parts
Value type description
Flag Uint8 0x01: Number of Steps (Required)
0x02: Distance (optional)
0x04: Calories (optional)
Such as 0x05 that contains the number of steps and calories
StepCount Uint24 The number of steps
StepDistancer Uint24 How far, in meters
StepCalorie Uint24 calories
Description:
1. Distance and calories are optional, may or may not appear
If only the number of steps, then the value is: 01 (steps) 10 27 00 (1 million steps)
If there are steps and distances, then the value is: 03 (number of steps, distance) 10 27 00 (1 million steps) 70 17 00 (6 km)
Other cases and so on.
2. Time value to mobile phone time as the standard, that is, the moment the phone receives the data that is the time of this data.
(3) target
The target value is
Value type description
Flag Uint8 0x01: Number of Steps (Required)
StepCount Uint24 The number of steps
Description:
1. If the target is 10,000 steps, then the value is: 01 (steps) 10 27 00 (1 million steps)
2. If the device writes to the target value, the device is updated. If the device updates the target value, notify the phone.
The reading I am getting from the pedometer watch is:
[7, 64, 1, 0, 144, 0, 0, 24, 0, 0]
Can anyone help me to interpret it?
The data appears to follow your description exactly. The first byte is the flag field and in this case it indicates that all 3 measurement types are being reported. Or`ing bits 1 and 2 and 3 equals 7.
The next 9 bytes are the 3 24-bit data values, where 64,1,0 are steps, 144,0,0 are distance, and 24,0,0 are calories. How you convert the bytes is a bit confusing, but if you use little-endian format and assume you printed them in decimal do these values make sense?
Steps: 00 01 64 = 0x000140 = 320
Distance: 00 00 144 = 0x000090 = 144
Calories: 00 00 24 = 0x000018 = 24
From your examples above the values are probably in hex:
10 27 00 = 0x002710 = 10000
70 17 00 = 0x001770 = 6000
Hope that helps!
grace

Resources