Alloy - Remove middle atom from tuple - tuples

I currently have three atoms class, pupil, grade and a tuple mark in the format class->pupil->grade. How can I return a tuple in the format class->grade so that I can see the grades a specific pupil has got in each class as a class/grade binary relation? Assume there is only one pupil in the system. Thanks!

One way I can imagine to achieve this is using set comprehension as follows:
fun getGrade[p:Pupil]: Class->Grade {
{c:Class,g:Grade|c->p->g in mark}
}

Related

Dungeons and Dragons Character Sheet generator using Python

For fun I'm trying to create a character generator sheet for Dungeons and Dragons. I've got the program to randomly roll for my strength, charisma etc.
Now I want to be able to ask the user, "What type of weapon do you want to use?" Get_Weapon_Choice, then pull up a list of that weapon type. I've tried creating a list weapon_Choices = ['Bow', 'Sword', ]. Then I created 2 other lists, bow = ['short', 'long', 'crossbow'] and swords = ['short', 'long'] I know how to get input from the user, but I don't know how to take that input and print the list, I'm printing the variable name.
chooseWeapon = input('What type of weapon would you like? Bow or Sword?')
How do I use chooseWeapon to compare to weapon_Choices to make sure they didn't enter something like Spells, then use chooseWeapon to print either the bow[] list or the swords[] list?
Do I need to use MySQL along with Python? and create tables and then search the tables instead of lists?
You can create a dictionary:
weapons = {"bow": ['short', 'long', 'crossbow'], "sword": ['short', 'long']}
# prompt user and convert to lowercase (our dict consists of lowercase strings)
chooseWeapon = input('What type of weapon would you like? Bow or Sword?').lower()
if chooseWeapon in weapons: # checks if the input is one of the keys in our dict
print(f'Available {chooseWeapon}s: {weapons[chooseWeapon]}')

How to iterate and store variables in a dictionary?

I am new to python and my coding experience so far is with MATLAB.
I am trying to understand more about lists and dictionaries as i am using a library about DOEs that takes an dictionary as a passing argument.
But my trouble so far is that this dictionary assumes the form of ex.
DOE={'Elastic Modulus':[10,20,30], 'Density':[1,2,3], 'Thickness':[2,3,5]}
But i need this dictionary to be user defined, for example:
Have an input to define how many variables are needed (in this example are 3: Elastic Modulus','Density'and 'Thickness)
as the variables are defined, it should be able to store values in the dictionary over a for loop.
Is this possible using dictionaries?
Or is it better to use a list and convert in a dicionary later?
Thank you in advance
One can add keys and the corresponding values to a dict one at a time like so:
my_dict = {}
num_entries = int(input("How many entries "))
for _ in range(num_entries):
key = input("Enter the key: ")
value = input("Enter the value: ")
my_dict[key] = value
Presumably you would have a loop to do the entry of key and value for the number of values you wish to enter. Also if you are in python 2 it needs to be raw_input rather than input function. [Edit: Showing how to do the loop, since I noticed that was part of your question]

compare two dictionary, one with list of float value per key, the other one a value per key (python)

I have a query sequence that I blasted online using NCBIWWW.qblast. In my xml blast file result I obtained for a query sequence a list of hit (i.e: gi|). Each hit or gi| have multiple hsp. I made a dictionary my_dict1 where I placed gi| as key and I appended the bit score as value. So multiple values for each key.
my_dict1 = {
gi|1002819492|: [437.702, 384.47, 380.86, 380.86, 362.83],
gi|675820360| : [2617.97, 2614.37, 122.112],
gi|953764029| : [414.258, 318.66, 122.112, 86.158],
gi|675820410| : [450.653, 388.08, 386.27] }
Then I looked for max value in each key using:
for key, value in my_dict1.items():
max_value = max(value)
And made a second dictionary my_dict2:
my_dict2 = {
gi|1002819492|: 437.702,
gi|675820360| : 2617.97,
gi|953764029| : 414.258,
gi|675820410| : 450.653 }
I want to compare both dictionary. So I can extract the hsp with the highest score bits. I am also including other parameters like query coverage and identity percentage (Not shown here). The finality is to get the best gi| with the highest bit scores, coverage and identity percentage.
I tried many things to compare both dictionary like this :
First code :
matches[]
if my_dict1.keys() not in my_dict2.keys():
matches[hit_id] = bit_score
else:
matches = matches[hit_id], bit_score
Second code:
if hit_id not in matches.keys():
matches[hit_id]= bit_score
else:
matches = matches[hit_id], bit_score
Third code:
intersection = set(set(my_dict1.items()) & set(my_dict2.items()))
Howerver I always end up with 2 types of errors:
1 ) TypeError: list indices must be integers, not unicode
2 ) ... float not iterable...
Please I need some help and guidance. Thank you very much in advance for your time. Best regards.
It's not clear what you're trying to do. What is hit_id? What is bit_score? It looks like your second dict is always going to have the same keys as your first if you're creating it by pulling the max value for each key of the first dict.
You say you're trying to compare them, but don't really state what you're actually trying to do. Find those with values under a certain max? Find those with the highest max?
Your first code doesn't work because I'm assuming you're trying to use a dict key value as an index to matches, which you define as a list. That's probably where your first error is coming from, though you haven't given the lines where the error is actually occurring.
See in-code comments below:
# First off, this needs to be a dict.
matches{}
# This will never happen if you've created these dicts as you stated.
if my_dict1.keys() not in my_dict2.keys():
matches[hit_id] = bit_score # Not clear what bit_score is?
else:
# Also not sure what you're trying to do here. This will assign a tuple
# to matches with whatever the value of matches[hit_id] is and bit_score.
matches = matches[hit_id], bit_score
Regardless, we really need more information and the full code to figure out your actual goal and what's going wrong.

How to handle probability notation in python?

I'm newbie for python 2.7. I would like to create some function that knows which are variables in the given probability notation.
For example: Given a probability P(A,B,C|D,E,F) as string input. The function should return a list of events ['A','B','C'] and a list of sample spaces ['D','E','F']. If it is impossible to return two lists in the same time. Returning a list of two lists would be fine.
In summary:
Input:
somefunction('P(A,B,C|D,E,F)')
Expected output: [['A','B','C'],['D','E','F']]
Thank you in advance
A simple brute-force implementation. As #fjarri pointed out if you want to do anything more complex you might need a parser (like PyParser) or at least some regular expressions.
def somefunction(str):
str = str.strip()[str.index("(")+1:-1]
left, right = str.split("|")
return [left.split(","), right.split(",")]

list of lists to either autovivify or recursive data structure

I have a list of lists in the format below. This is data coming from a csv and I am trying to emulate the data review function that excel has in python. The only reason I can't do it directly in excel is this document is almost 1GB and has 1.1 mil row.
((a1,b1,c1,d1,e1),(a1,b2,c1,d2,e2),(a1,b1,c2,d3,e3),(a2,b1,c1,d3,e4),(a2,b2,c2,d3,e5)...)
I want to convert it into a single data structure something like a multidimensional array. like below
((a1:(b1:(c1:(),c2:()),b2:(),b3:()),a2:(b1:(c1:()),b2:(c2:()),b3:())))
I use autovivify class for other purposes but I can't use it here because some of the keys I want to use are strings. Appreciate help here.
If I understand your question correctly, you want to transform that list into a tree-like structure, where each tuple in the list represents one path down the tree. You can do this using nested dictionaries:
def add_to_dict(d, t):
if t:
first, rest = t[0], t[1:]
nested = d.setdefault(first, {})
add_to_dict(nested, rest)
Given a dictionary d (initially empty) and one of those tuples t, if that tuple is not empty, it takes the first element from the tuple, adds a nested dictionary to the original dictionary using this element as key (or takes one that already exists in this place), and adds the rest of the tuple to that dictionary in the same way.
Example using your data:
data = (('a1','b1','c1','d1','e1'),
('a1','b2','c1','d2','e2'),
('a1','b1','c2','d3','e3'),
('a2','b1','c1','d3','e4'),
('a2','b2','c2','d3','e5'))
d = {}
for t in data:
add_to_dict(d, t)
The resulting dictionary d looks like this:
{'a1': {'b1': {'c1': {'d1': {'e1': {}}},
'c2': {'d3': {'e3': {}}}},
'b2': {'c1': {'d2': {'e2': {}}}}},
'a2': {'b1': {'c1': {'d3': {'e4': {}}}},
'b2': {'c2': {'d3': {'e5': {}}}}}}