|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On October 24 2016 03:08 Nesserev wrote:Show nested quote +On October 24 2016 02:35 travis wrote: Okay let's say I was using a set to store objects. And, if I wanted to store a repeat of the same object, I wanted to increase some sort of count that said "now you're storing 2 of those objects". So Let's say if I added "3, 4, 4, 4, 3" to the bag, I would be able to see "3" and "4" in the bag, and somehow have a count that could tell me that there was 2 3s, and 3 4s.
How could I do that in java?
edit: what is coming to my mind first is putting the items into an array of size 2, where one of the indexes is the object and the other index is the number of the objects
and then putting the arrays into the set
edit: or I guess I could make a new class that has the object and the count and add that into the set? Just use Map<T, Integer>. Increase the second value by one when adding an object. Decrease the second value by one when removing an object.
I thought about this,
can't remember what convinced me not to do it I don't see a problem with it now  so yeah I'll try this
|
On October 24 2016 02:35 travis wrote: Okay let's say I was using a set to store objects. And, if I wanted to store a repeat of the same object, I wanted to increase some sort of count that said "now you're storing 2 of those objects". So Let's say if I added "3, 4, 4, 4, 3" to the bag, I would be able to see "3" and "4" in the bag, and somehow have a count that could tell me that there was 2 3s, and 3 4s.
How could I do that in java?
edit: what is coming to my mind first is putting the items into an array of size 2, where one of the indexes is the object and the other index is the number of the objects
and then putting the arrays into the set
edit: or I guess I could make a new class that has the object and the count and add that into the set?
Do you really have to store the object count though? If you want to know how many duplicates of an object/value are in a list you can always check it with this:
ArrayList<String> animals = new ArrayList<String>(); animals.add("bat"); animals.add("owl"); animals.add("bat"); animals.add("bat");
int occurrences = Collections.frequency(animals, "bat"); // will return 3
|
yeah I have to. Part of my specifications are to not have duplicate items but retain a count of them instead.
|
Okay, so I am using a hashtable now.
My project has me write my own iterator, which is a little bit confusing for me.
Not really sure how to write iterator methods (specifically next() and remove()) to go through a hashtable
|
It seems to me you're not using Google enough 
Big part of the fun with programming is doing the research and trying to figure things out yourself (especially simple ones like that).
|
And don't use a hashtable unless you have a good reason. Just go with hashmap.
|
On October 24 2016 04:50 Manit0u wrote:It seems to me you're not using Google enough  Big part of the fun with programming is doing the research and trying to figure things out yourself (especially simple ones like that).
That's really not true. Particularly for this. There is next to no explanation how I could implement my own iterator for a hashtable. At least that I could find, and I did quite a bit of looking.
On October 24 2016 04:55 ShAsTa wrote: And don't use a hashtable unless you have a good reason. Just go with hashmap.
ok hashmap it is then...
Since I am writing an iterator should I use linkedhashmap ?
|
Only if the order of the elements is important.
|
On October 24 2016 04:57 travis wrote:Show nested quote +On October 24 2016 04:50 Manit0u wrote:It seems to me you're not using Google enough  Big part of the fun with programming is doing the research and trying to figure things out yourself (especially simple ones like that). That's really not true. Particularly for this. There is next to no explanation how I could implement my own iterator for a hashtable. At least that I could find, and I did quite a bit of looking.
Well, I'm no Java expert but it seems to me that you need to iterate over the map.entrySet(), which already has its own iterator.
Sample from one of the top 3 entries in Google search:
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) { Map.Entry<Integer, String> entry = iterator.next();
System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
iterator.remove(); // right way to remove entries from Map, avoids ConcurrentModificationException }
|
That's what I ended up using in the end. Though it gets a little more complicated because I need to wait to call .next() until I've iterated through all the duplicates of each key.
As for it being on the top 3 results, what I was really looking for (at least initially), was how to do it without just calling the methods of another iterator.
|
On October 24 2016 07:29 travis wrote: That's what I ended up using in the end. Though it gets a little more complicated because I need to wait to call .next() until I've iterated through all the duplicates of each key.
As for it being on the top 3 results, what I was really looking for (at least initially), was how to do it without just calling the methods of another iterator.
Why do you have to wait? Wasn't the entire point of the exercise making it so that you store a key and a number of it's duplicates as a value?
As in:
[ "somekey" : 2, "otherkey" : 1, ]
Then, if you need to add the same key you just increment the value. If you remove one you decrement it and if it's now 0 you remove the element entirely.
This way you always have the same key only once inside your map, all that changes is the value it holds.
It's all about overriding the add and remove methods on your map, so that it behaves as you'd like.
|
On October 24 2016 09:27 Manit0u wrote:Show nested quote +On October 24 2016 07:29 travis wrote: That's what I ended up using in the end. Though it gets a little more complicated because I need to wait to call .next() until I've iterated through all the duplicates of each key.
As for it being on the top 3 results, what I was really looking for (at least initially), was how to do it without just calling the methods of another iterator. Why do you have to wait? Wasn't the entire point of the exercise making it so that you store a key and a number of it's duplicates as a value? As in: [ "somekey" : 2, "otherkey" : 1, ]
Then, if you need to add the same key you just increment the value. If you remove one you decrement it and if it's now 0 you remove the element entirely. This way you always have the same key only once inside your map, all that changes is the value it holds. It's all about overriding the add and remove methods on your map, so that it behaves as you'd like.
sure for add or remove methods it's this easy
but I am talking about the next method of a custom iterator
if i am using my custom iterator to iterate through with a for each loop, and say - do a print on each element
simply using the .next() of the map's iterator won't print out the duplicates. I could print out the keys but I have to do something custom so that it iterates out those keys an amount of times proportional to the value".
like, in your example, I would want my for each loop with a print statement to print somekey, somekey, otherkey
|
Can you please give me design feedback on this python code? In particular, I am not sure if I should be instantiating Creatures in the level grid, if a Door should be a Creature, and how to do unit collision.
I also would like to know how I can tie the Creature's movement patterns to the Creature, and not to the level. If I try to make it a Creature function, then it will be missing necessary level data, like where the Player is, right?
Any other design criticism, particularly about the division of functions and values is definitely helpful. Thanks!
|
On October 24 2016 11:04 travis wrote:Show nested quote +On October 24 2016 09:27 Manit0u wrote:On October 24 2016 07:29 travis wrote: That's what I ended up using in the end. Though it gets a little more complicated because I need to wait to call .next() until I've iterated through all the duplicates of each key.
As for it being on the top 3 results, what I was really looking for (at least initially), was how to do it without just calling the methods of another iterator. Why do you have to wait? Wasn't the entire point of the exercise making it so that you store a key and a number of it's duplicates as a value? As in: [ "somekey" : 2, "otherkey" : 1, ]
Then, if you need to add the same key you just increment the value. If you remove one you decrement it and if it's now 0 you remove the element entirely. This way you always have the same key only once inside your map, all that changes is the value it holds. It's all about overriding the add and remove methods on your map, so that it behaves as you'd like. sure for add or remove methods it's this easy but I am talking about the next method of a custom iterator if i am using my custom iterator to iterate through with a for each loop, and say - do a print on each element simply using the .next() of the map's iterator won't print out the duplicates. I could print out the keys but I have to do something custom so that it iterates out those keys an amount of times proportional to the value". like, in your example, I would want my for each loop with a print statement to print somekey, somekey, otherkey
In general, the hashmap does not implement the Iterable iterface. That is why your are not finding documentaion on it. It does not make sense for a hashmap to implement the iterable interface. What would it return? the keys or the values? How can you you tell is what to return by default?
The hashmap will usually have a keyset() method or keys() method that will return all the keys of the hashmap.
Additionally, you need to understand that the iterator is an abstraction to loop over items.
For all practical purposes, you can think of the iterable interface basically allowing your class to do the following:
//You can write this piece of code, which is what some people have written Iterable<String> it = myMap.iterator() while ( it.hasNext() ) { String next = it.next() // your code here }
//Or you can write this piece of code, and the compiler will automatically convert it to the code above. //Normally, they call this syntactic sugar, as the below piece of code is significantly easier to read and write. HashMap<String,Interger> myMap = new HashMap<String,Interger>()
foreach(String key : myMap.keySet() ) { //your code here }
Also note, that the hashmap basically has two types of implementations 1. SeperateChaining (performance degrades gracefully, less sensitive to badly designed hash function) 2. LinearProbing (less wasted space, better cache performance)
The key part of the hashmap is the HASH function. Meaning, the key loopups are done using a Hash Function. This is implemented using the equals and hashcode method in java.
You have the option to override the objects hash function so that you can group them into unique buckets. Obviously, you wont do this for primitive types, but for custom objects, you want to override the hash function if you plan on using them in a hashmap.
There are some basic rules you want to follow. However, writing a good hash function is a skill in itself deserving a PHD thesis.
1. Hash functions should be immutable. If your object keeps mutating, it will never find the object. 2. Should lead to good performance. Shoudl spread your data out (Make sure it can be bucketed in both even and odd intervals and is uniformly distributed. ) 3. Easy to store and easy to evaluate. 4. Make sure to test out your hash implementation with your dataset. There are certain pathological datasets that will destroy your hash implementation.
There are several types of hash functions and each of them do thier own thing really well. (ie. crytography)
|
On October 24 2016 11:04 travis wrote:Show nested quote +On October 24 2016 09:27 Manit0u wrote:On October 24 2016 07:29 travis wrote: That's what I ended up using in the end. Though it gets a little more complicated because I need to wait to call .next() until I've iterated through all the duplicates of each key.
As for it being on the top 3 results, what I was really looking for (at least initially), was how to do it without just calling the methods of another iterator. Why do you have to wait? Wasn't the entire point of the exercise making it so that you store a key and a number of it's duplicates as a value? As in: [ "somekey" : 2, "otherkey" : 1, ]
Then, if you need to add the same key you just increment the value. If you remove one you decrement it and if it's now 0 you remove the element entirely. This way you always have the same key only once inside your map, all that changes is the value it holds. It's all about overriding the add and remove methods on your map, so that it behaves as you'd like. sure for add or remove methods it's this easy but I am talking about the next method of a custom iterator if i am using my custom iterator to iterate through with a for each loop, and say - do a print on each element simply using the .next() of the map's iterator won't print out the duplicates. I could print out the keys but I have to do something custom so that it iterates out those keys an amount of times proportional to the value". like, in your example, I would want my for each loop with a print statement to print somekey, somekey, otherkey
Are you just talking about something like...
idk how iterator.next() works :D
<redacted>
|
The whole point of the assignment is to make sure that you understand what an iterator does. Once you do, it will be very easy. Do yourself a favor and take the time until you get the ah ah moment.
|
"Now that your data extraction script works flawlessly with 50 people, it will also work flawlessly with the entire population of Lagos, right?"
#JustNonTechnicalProductManagerThings
|
On October 23 2016 10:30 Hhanh00 wrote: The 'random' part is a placeholder for code that he means to replace with something better.
@OP, your code seems fine but uses algo that aren't common. It is bound to raise some questions. 1. The session key is hash(k1|k2). Assuming k1 and k2 have enough entropy, sk is ok but why not use a standard key derivation scheme instead? 2. You use MCRYPT_RIJNDAEL_256. I'm not sure if it's on purpose but if you intended to do AES-256, you should use MCRYPT_RIJNDAEL_128. 128 refers to the block size and not the key size. 3. mcrypt pads with 0 if the data isn't a multiple of the block size. If your data can have trailing \0, this could be problematic. 4. You have mac on plain text then encrypt. That doesn't protect the ciphertext. The recommended way is to encrypt and then add mac on ciphertext.
Disclaimer: I know next to nothing about PHP so I can't comment on that and I'm not an expert in crypto either.
Thanks for all the help. Just to address #2: RIJNDAEL is an algorithm used by AES 
Since libsodium is a no-go for me (fucking govt issued servers) I've decided to go with openssl instead.
Would be thankful if anyone could double-check this code as valid for encrypting/decrypting sensitive data for storage purposes (like personal data inside a database):
abstract class Crypto { const CIPHER = 'aes-256-cbc'; const HMAC_ALGO = 'sha256';
/** * Number of bytes generated for encryption key * 64 = 512-bit * 32 = 256-bit * 16 = 128-bit */ const CSPRNG_BYTES = 32; /** * Number of characters generated for HMAC (hex) */ const HMAC_XBYTES = 64;
/** * @param string $data - data to encrypt * @param string|null $encryptionKey - hex * @return string - encrypted data */ final public static function encrypt($data, $encryptionKey = null) { if (null === $encryptionKey) { $encryptionKey = static::getEncryptionKey(); }
$data .= static::getHMAC($data, $encryptionKey); // add authentication code $iv = static::getInitializationVector();
$encrypted = openssl_encrypt( $data, self::CIPHER, hex2bin($encryptionKey), OPENSSL_RAW_DATA, $iv );
return sprintf('%s|%s', base64_encode($encrypted), base64_encode($iv)); }
/** * @param string $encrypted - encrypted data * @param string|null $encryptionKey - hex * @return bool|string - decrypted data or false on failure */ final public static function decrypt($encrypted, $encryptionKey = null) { list($data, $iv) = explode('|', $encrypted);
if (null === $encryptionKey) { $encryptionKey = static::getEncryptionKey(); }
$decrypted = openssl_decrypt( base64_decode($data), self::CIPHER, hex2bin($encryptionKey), OPENSSL_RAW_DATA, base64_decode($iv) );
if (!static::validHMAC($decrypted, $encryptionKey)) { return false; }
return substr($decrypted, 0, -self::HMAC_XBYTES); }
/** * Generate a keyed-hash message authentication code (HMAC) * * @param string $data - data to be encrypted * @param string $key - encryption key (hex) * @return string */ final public static function getHMAC($data, $key) { return hash_hmac(self::HMAC_ALGO, $data, $key); }
/** * @param $data - decrypted data with hmac * @param $key - encryption key (hex) * @return bool */ final public static function validHMAC($data, $key) { $expected = static::getHMAC(substr($data, 0, -self::HMAC_XBYTES), $key); $hmac = substr($data, -self::HMAC_XBYTES);
return $hmac === $expected; }
/** * Generate a fixed-size input to a cryptographic primitive * * @return string - binary string */ final public static function getInitializationVector() { return openssl_random_pseudo_bytes(static::getVectorSize()); }
/** * Get size of starting value for specific cipher * * @return int */ final public static function getVectorSize() { return openssl_cipher_iv_length(self::CIPHER); }
/** * Use cryptographically secure pseudo-random number generator (CSPRNG) * to generate a desired length of bytes * * @return string - binary string */ final public static function generateRandomKey() { return openssl_random_pseudo_bytes(self::CSPRNG_BYTES); } }
|
On October 24 2016 19:52 Djagulingu wrote: "Now that your data extraction script works flawlessly with 50 people, it will also work flawlessly with the entire population of Lagos, right?"
#JustNonTechnicalProductManagerThings It will work fine. People in Lagos are very used to shit not working.
|
On October 24 2016 21:10 Acrofales wrote:Show nested quote +On October 24 2016 19:52 Djagulingu wrote: "Now that your data extraction script works flawlessly with 50 people, it will also work flawlessly with the entire population of Lagos, right?"
#JustNonTechnicalProductManagerThings It will work fine. People in Lagos are very used to shit not working. In the defense of the Product Manager, it did work fine. Not before me changing 80 lines of a 100 line script though.
|
|
|
|