So I'm defining a variable/function that takes two inputs and displays a series of 1's and 0's
bin 0 0 = '0'
bin 0 1 = '1'
bin 0 2 = '1'
bin 0 3 = '0'
bin 0 4 = '1'
now I want to create a duplicate of the bin variable except that at 0 3 there should be a 1 so I tried to achieve this in a new function
changeBin w z = binNew where
binNew w z = '1'
binNew x y = bin x y
yet if I do it like this it gives me a pattern match redundant warning and when I call changeBin 0 3 it gets into a loop but when I change the function to
changeBin w z = binNew where
binNew 0 3 = '1'
binNew x y = bin x y
this works but I want to do it the first way so I can change it anytime without writing a whole function but I dont know why it gives me a redundant error when I write the same just with the numbers it works
I am new to haskell bear with me thanks
any help is appreciated on what my error is on the first function
1 changeBin w z = binNew where
2 binNew w z = '1'
3 binNew x y = bin x y
The w on line 1 and the w on line 2 are different variables. w on line 2 does not become a pattern that only matches when it has the same value as the w given on line one –– instead it defines a new variable that shadows the old one. You need to explicitly compare:
changeBin w z = binNew
where
binNew w' z'
| w' == w && z' == z = '1'
| otherwise = bin x y
Related
Definition of H Index used in this algorithm
Supposing a relational expression is represented as y = F(x1, x2, . . . , xn), where F returns an integer number greater than 0, and the function is to find a maximum value y satisfying the condition that there exist at least y elements whose values are not less than y. Hence, the H-index of any node i is defined as
H(i) = F(kj1 ,kj2 ,...,k jki)
where kj1, kj2, . . . , kjki represent the set of degrees of neighboring nodes of node i.
Now I want to find the H Index of the nodes of the following graphs using the algorithm given below :
Graph :
Code (Written in Python and NetworkX) :
def hindex(g, n):
nd = {}
h = 0
# print(len(list(g.neighbors(n))))
for v in g.neighbors(n):
#nd[v] = len(list(g.neighbors(v)))
nd[v] = g.degree(v)
snd = sorted(nd.values(), reverse=True)
for i in range(0,len(snd)):
h = i
if snd[i] < i:
break
#print("H index of " + str(n)+ " : " + str(h))
return h
Problem :
This algorithm is returning the wrong values of nodes 1, 5, 8 and 9
Actual Values :
Node 1 - 6 : H Index = 2
Node 7 - 9 : H Index = 1
But for Node 1 and 5 I am getting 1, and for Node 8 and 9 I am getting 0.
Any leads on where I am going wrong will be highly appreciated!
Try this:
def hindex(g, n):
sorted_neighbor_degrees = sorted((g.degree(v) for v in g.neighbors(n)), reverse=True)
h = 0
for i in range(1, len(sorted_neighbor_degrees)+1):
if sorted_neighbor_degrees[i-1] < i:
break
h = i
return h
There's no need for a nested loop; just make a decreasing list, and calculate the h-index like normal.
The reason for 'i - 1' is just that our arrays are 0-indexed, while h-index is based on rankings (i.e. the k largest values) which are 1-indexed.
From the definition of h-index: For a non-increasing function f, h(f) is max i >= 0 such that f(i) >= i. This is, equivalently, the min i >= 1 such that f(i) < i, minus 1. Here, f(i) is equal to sorted_neighbor_degrees[i - 1]. There are of course many other ways (with different time and space requirements) to calculate h.
I need to parse this data so that each value in the data parsing column is deposited in its own column.
userid data_to_parse
0 54f3ad9a29ada "value":"N;U;A7;W"}]
1 54f69f2de6aec "value":"N;U;I6;W"}]
2 54f650f004474 "value":"Y;U;A7;W"}]
3 54f52e8872227 "value":"N;U;I1;W"}]
4 54f64d3075b72 "value":"Y;U;A7;W"}]
So for example, the four additional columns for the first entry would have values of “N”, “U”, “A7”, and “W”. I first attempted to split based upon index like so:
parsing_df['value_one'] = parsing_df['data_to_parse'].str[9:10]
parsing_df['value_two'] = parsing_df['data_to_parse'].str[11:12]
parsing_df['value_three'] = parsing_df['data_to_parse'].str[13:15]
parsing_df['value_four'] = parsing_df['data_to_parse'].str[16:17]
This worked really well except that there are a few that are different lengths like 937 and 938.
935 54f45edd13582 "value":"N;U;A7;W"}] N U A7 W
936 54f4d55080113 "value":"N;C;A7;L"}] N C A7 L
937 54f534614d44b "value":"N;U;U;W"}] N U U; "
938 54f383ee53069 "value":"N;U;U;W"}] N U U; "
939 54f40656a4be4 "value":"Y;U;A1;W"}] Y U A1 W
940 54f5d4e063d6a "value":"N;U;A4;W"}] N U A4 W
Does anyone have any solutions that doesn't utilize hard-coded positions?
Thanks for the help!
A relatively simple way to approach the problem:
txt = """54f45edd13582 "value":"N;U;A7;W"}]
54f4d55080113 "value":"N;C;A7;L"}]
54f534614d44b "value":"N;U;U;W"}]
54f383ee53069 "value":"N;U;U;W"}]
54f40656a4be4 "value":"Y;U;A1;W"}]
54f5d4e063d6a "value":"N;U;A4;W"}]
"""
import pandas as pd
txt = txt.replace('}','').replace(']','').replace('"','') #first, clean up the data
#then, collect your data (it may be possible to do it w/ list comprehension, but I prefer this):
rows = []
for l in [t.split('\tvalue:') for t in txt.splitlines()]:
#depending on your actual data, you may have to split by "\nvalue" or " value" or whatever
row = l[1].split(';')
row.insert(0,l[0])
rows.append(row)
#define your columns
columns = ['userid','value_one','value_two','value_three','value_four']
#finally, create your dataframe:
pd.DataFrame(rows,columns=columns)
Output (pardon the formatting):
userid value_one value_two value_three value_four
0 54f45edd13582 N U A7 W
1 54f4d55080113 N C A7 L
2 54f534614d44b N U U W
3 54f383ee53069 N U U W
4 54f40656a4be4 Y U A1 W
5 54f5d4e063d6a N U A4 W
str.split(':')
E.g.
chars = parsing_df['data_to_parse']split(':')
parsing_df['value_one'] = chars[0]
...
for i, char in enumerate(parsing_df['data_to_parse']split(':')):
pass
# use i to get the column and then set it to char
I'm working on a project for my Assembly Language class and I can't seem to figure out what I'm doing wrong and why my code is being outputted as reversed order and why I'm having the -1 answer displayed as an output.
Here are the instructions for the project:
This final version should read in all values and AFTER the -1 is
entered, it should display all of the numbers entered followed by
printing out the sum of these numbers. The stack must be used for this
program.
Here is my code so far:
START: READ X
PUSH
LOAD X
STACKW 0
LOAD Z
ADD 1
STORE Z
LOAD X
BRNEG OUT
BR SV
SV: LOAD Y
ADD X
STORE Y
BR WAIT
WAIT: NOOP
BR START
OUT: LOAD X
ADD 1
BRZERO EH
BRNEG SV
EH: STACKR 0
STORE W
WRITE W
POP
BR ENDOUTCOUNT
ENDOUTCOUNT: LOAD Z
SUB 1
STORE Z
LOAD Z
BRZERO END
BR EH
END: WRITE Y
NOOP
STOP
Z 0
Y 0
X 0
W 0
Here is my current output: I'd like to display the output numbers in ascending order and also hide the -1 as the output. All help would be appreciated.
Let's say I get a list, and I want to iterate on three at a time. I.e.: I have a list with [1,4,5,6,7,8,-9,2,0] In TCL, I can just use (for example):
foreach { x y z } $list {
puts "x is ${x}"
puts "y is ${y}"
puts "z is ${z}"
}
How can I define more than 1 variable, using the for loop with the in (array name) in Python 3.3? The Python org wiki showed just example of 1 iteration variable. This is also easily done in RubyThanks.
EDIT: The expected output is:
x is 1
y is 4
z is 5
x is 6
y is 7
z is 8
x is -9
y is 2
z is 0
You can split this array into chunks, and then just do something like this:
for x,y,z in chunked_array:
print("x=", x)
print("y=", y)
print("z=", z)
I would like to count the number of occurrences of an event (for example, x data value equals some number) and store these occurrences in order, while plotting a file in gnuplot. Say I have the following file:
1
0
0
0
1
1
0
Now I want to count how many times I have a 1 and store that number in variable N. Then I want to know the positions where that happens and store that information in an array pos, all of this while plotting the file. The result, for the example above, should be:
print N
3
print pos
1 5 6
I know how to achieve the counting:
N = 0
plot "data" u ($0):($1 == 1 ? (N = N+1, $1) : $1)
print N
3
Then to achieve the position recording, it would be schematically something like this:
N = 0 ; pos = ""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = pos." ".$0, $1) : $1) # This doesn't work!
print N
3
print pos
1 5 6
How can this be done in gnuplot without resorting to external bash commands?
Well, as sometimes happens writing down the question triggers an idea for an answer. I'll leave it here in case somebody finds it useful:
N=0 ; pos=""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = sprintf("%s %g", pos, $0+1), $1) : $1)
print N
3
print pos
1 5 6
Note I had to use $0+1 because position 1 is treated by gnuplot as zero.