NSDictionary frome one key to many [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I got NSDictionary
Printing description of gameDict:
{
result = {
developer = "Bethesda Game Studios";
genre = "Role-Playing";
name = "The Elder Scrolls V: Skyrim";
platform = "PlayStation 3";
publisher = "Bethesda Softworks";
rating = M;
rlsdate = "2011-11-11";
score = 92;
summary = "The next chapter in the Elder Scrolls saga arrives from the Bethesda Game Studios. Skyrim reimagines the open-world fantasy epic, bringing to life a complete virtual world open for you to explore any way you choose. Play any type of character you can imagine, and do whatever you want; the legendary freedom of choice, storytelling, and adventure of The Elder Scrolls is realized like never before. Skyrim's new game engine brings to life a complete virtual world with rolling clouds, rugged mountains, bustling cities, lush fields, and ancient dungeons. Choose from hundreds of weapons, spells, and abilities. The new character system allows you to play any way you want and define yourself through your actions. Battle ancient dragons like you've never seen. As Dragonborn, learn their secrets and harness their power for yourself.";
thumbnail = "http://static.metacritic.com/images/products/games/6/372529409ea6cc3faadf5674fd96ba2a-98.jpg";
url = "http://www.metacritic.com/game/playstation-3/the-elder-scrolls-v-skyrim";
userscore = "6.3";
};
}
It got only one key "result"
How can i get other keys?

gameDict is an NSDictionary with one key/pair. The key "result" points to another NSDictionary which has lots more keys. So to access any of the deeper keys you can either do it inline:
NSNumber *score = gameDict[#"result"][#"score"];
Or you can pull out the Dictionary and query it
NSDictionary *resultDict = gameDict[#"result"];
NSNumber *score = resultDict[#"score"];

Related

Why does DocumentFile.getName take a relatively long time? [duplicate]

This question already has answers here:
Why is DocumentFile so slow, and what should I use instead?
(5 answers)
Closed 1 year ago.
I have seen posts describing DocumentFile.listFiles() takes a long time. In my case, it is fairly fast but operations on the retrieved instances of DocumentFile take a long time.
DocumentFile[] aFiles = dfDir.listFiles(); //Takes 100 ms with a list of 250 files
//The following takes 5,000ms
for (DocumentFile df : aFiles) {
boolean bFoo = df.isDirectory(); //takes about 10ms
String sName = df.getName(); //takes about 10ms
}
Could anyone shed some light on this?
getName() invokes ContentResolver#query() under the hood ... [this] performs hundreds of queries, which is very inefficient."
From an answer to the duplicate question you linked to.

spaCy: optimizing tokenization

I'm currently trying to tokenize a text file where each line is the body text of a tweet:
"According to data reported to FINRA, short volume percent for $SALT clocked in at 39.19% on 12-29-17 http://www.volumebot.com/?s=SALT"
"#Good2go #krueb The chart I posted definitely supports ng going lower. Gobstopper' 2.12, might even be conservative."
"#Crypt0Fortune Its not dumping as bad as it used to...."
"$XVG.X LOL. Someone just triggered a cascade of stop-loss orders and scooped up morons' coins. Oldest trick in the stock trader's book."
The file is 59,397 lines long (a day's worth of data) and I'm using spaCy for pre-processing/tokenization. It's currently taking me around 8.5 minutes and I was wondering if there were any way of optimising the following code to be quicker as 8.5 minutes seems awfully long for this process:
def token_loop(path):
store = []
files = [f for f in listdir(path) if isfile(join(path, f))]
start_time = time.monotonic()
for filename in files:
with open("./data/"+filename) as f:
for line in f:
tokens = nlp(line.lower())
tokens = [token.lemma_ for token in tokens if not token.orth_.isspace() and token.is_alpha and not token.is_stop and len(token.orth_) != 1]
store.append(tokens)
end_time = time.monotonic()
print("Time taken to tokenize:",timedelta(seconds=end_time - start_time))
return store
Although it says files, it's currently only looping over 1 file.
Just to note, I only need this to tokenize the content; I don't need any extra tagging etc.
It sounds like you haven't optimised the pipeline yet. You'll get a significant speed up from disabling the pipeline components you don't need, like so:
nlp = spacy.load('en', disable=['parser', 'tagger', 'ner'])
This should get you down to about the two-minute mark, or better, on its own.
If you need a further speed up, you can look at multi-threading using nlp.pipe. Docs for multi-threading are here:
https://spacy.io/usage/processing-pipelines#section-multithreading
You can use nlp.pipe(all_lines) instead of nlp(line) for a faster processing
see Spacy's documentation - https://spacy.io/usage/processing-pipelines

Lua: Working with the Modbus TCP/IP Protocol

This question is based off a previous question I asked concerning a similar topic: Lua: Working with Bit32 Library to Change States of I/O's . I'm trying to use a Lua program that, when a PLC changes the state of a coil at a given address (only two addresses will be used) then it triggers a reaction in another piece of equipment. I have some code that is basically the exact same as my previous topic. But this has to do with what this code is actually doing and not so much the bit32 library. Usually I run code I don't in understand in my Linux IDE and slowly make changes until I finally can make sense of it. But this is producing some weird reactions that I can't make sense of.
Code example:
local unitId = 1
local funcCodes = {
readCoil = 1,
readInput = 2,
readHoldingReg = 3,
readInputReg = 4,
writeCoil = 5,
presetSingleReg = 6,
writeMultipleCoils = 15,
presetMultipleReg = 16
}
local function toTwoByte(value)
return string.char(value / 255, value % 255)
end
local coil = 1
local function readCoil(s, coil)
local req = toTwoByte(0) .. toTwoByte(0) .. toTwoByte(6) .. string.char(unitId, funcCodes.readCoil) .. toTwoByte(coil - 1) .. toTwoByte(1)
s:write(req) --(s is the address of the I/O module)
local res = s:read(10)
return res:byte(10) == 1 -- returns true or false if the 10th bit is ==1 I think??? Please confirm
end
The line that sets local req is the part I'm truly not making sense of. Because of my earlier post, I understand fully about the toTwoByte function and was quickly refreshed on bits & byte manipulation (truly excellent by the way). But that particular string is the reason for this confusion. If I run this in the demo at lua.org I get back an error "lua number has no integer representation". If I separate it into the following I am given back ascii characters that represent those numbers (which I know string.char returns the ascii representation of a given digit). If I run this in my Linux IDE, it displays a bunch of boxes, each containing four digits; two on top of the other two. Now it is very hard to distinguish all of the boxes and their content as they are overlapping.
I know that there is a modbus library that I may be able to use. But I would much rather prefer to understand this as I'm fairly new to programming in general.
Why do I receive different returned results from Windows vs Linux?
What would that string "local req" look like when built at this point to the I/O module. And I don't understand how this req variable translates into the proper string that contains all of the information used to read/write to a given coil or register.
If anyone needs better examples or has further questions that I need to answer, please let me know.
Cheers!
ETA: This is with the Modbus TCP/IP Protocol, not RTU. Sorry.

Generate random number & after adding/subtract them comparing the result in objective c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
As I'm from other scripting language background, I'm not so much expert in objective-c & iPhone programming.
I've tried some code of objective-c myself, but they aren't working as I expected.
I have elaborated my requirements on this image:
So I'm seeking a help from the experts about my program-
I want to make a program for iPhone in objective-C that will generate random number & generate signs (like +, -, *, /).
Then it will subtract/add/multiply/divide with random values in 2 different sections.
Now users have to compare between two sections.
Is the result of 2 sections are = or < or > . There will be three individual buttons for the signs.
After getting the press on the button from the user a new label will show the answer, if it is right/wrong.
If the answer is wrong, it will show the correct answer also.
Any help on this program on objective-c language required.
Thanks in advance.
To generate a random number in a range use:
int randomInteger = arc4random_uniform(range);
To generate a random operator character:
NSArray *operators = #[#"+", #"-", #"*", #"/"];
int randomOperatorIndex = arc4random_uniform(4);
NSString *randomOperator = operators[randomOperatorIndex];
The rest of the code you need to figure out and write yourself. If you have problems post another question with the problem code for help with it.
Notes:
arc4random() is a high quality 32-bit pseudo-random number generator that does not need to be seeded, it seeds itself on a regular basis from the kernel strong random number subsystem.
arc4random_uniform(range) returns an integer in the range of 0 to (range-1). It has the added benefit that it avoids "modulo bias" that the mod (%) operator does.
Use arc4random() to generate random no.
Use following to generate random operator
-(NSString*) getRamdomOperator
{
int randNum = arc4random_uniform(4);
NSString *operator;
switch(randNum)
{
case 0:
operator = #"+";
break;
case 1:
operator = #"-";
break;
case 2:
operator = #"/";
break;
case 3:
operator = #"*";
break;
Default:
break;
}
return operator;
}

Looping through NSArray while subtracting values from each object? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have NSNumber objects stored in an NSMutableArray. I am attempting to perform a calculation on each object in the array.
What I want to do is:
1) Take a random higher number variable and keep subtracting a smaller number variable in increments until the value of the variable is equal to the value in the array.
For example:
NSMutableArray object is equal to 2.50.
I have an outside variable of 25 that is not in the array.
I want to subtract 0.25 multiple times from the variable until I reach less than or equal to 2.50. I also need a parameter so if the number does not divide evenly and goes below the array value, it resorts to the original array value of 2.50.
Lastly, for each iteration, I want to print the values as they are counting down as a string.
I was going to provide code, but I don't want to make this more confusing than it has to be.
So my output would be:
VALUE IS: 24.75
VALUE IS: 24.50
VALUE IS: 24.25
…
VALUE IS: 2.50
END
I'm on my iPad so this won't be as nice as I would like
float randomValue = 25; //or however you get the value
float subtractionValue = 0.25 // or whatever value or use a define
for (NSNumber *n in myArray)
{
while (randomValue > n.floatvalue)
{
randomValue -= subtractionValue;
NSLog(#"Value is: %f", randomValue);
if (randomValue < 2.5)
{
randomValue = n.floatvalue;
}
}
}
Well run the while loop and check like that below:-
Lets assume your variable here whose value is 25
float a=25;
while (a>=2.50)
{
a=a-0.25;
NSlog(#"count is %f",a);
}
Hope it helps, it is just the logic you can try with your code and if required please modify accordingly.

Resources