I'm dealing with a problem which needs to work with a lot of data. Currently its' values are represented as unsigned int. I know that real values do not exceed some limit, say 1000. That means that I can use unsigned short to store it. One profit is that it'll use less space. Do I have to pay for it by loosing in performance?
Another assumption. I decided to store data as short but all calling functions use int, so I need to convert between these datatypes when storing/extracting values. Wiil the performance lost be dramatic?
Third assumption. Due to great wish to econom memory I decided to use not short but just 10 bits packed into array of unsigned int. What will happen in this case comparing with previous ones?
I resized an image using Java2D Graphics class. But it doesn't look right.
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
Is it possible to scale an image without introducing artifacts?
In my bash script I have an external (received from user) string, which I should use in sed pattern.
REPLACE="<funny characters here>"
sed "s/KEYWORD/$REPLACE/g"
How can I escape the $REPLACE string so it would be safely accepted by sed as a literal replacement?
NOTE: The KEYWORD is a dumb substring with no matches etc. It is not supplied by user.
I have this source code where I got it from net tutsplus. I have configured it and made it work in one PHP file. It does work by transferring the original image, but it does not generate to the thumbnails folder.
<?php
$final_width_of_image = 100;
$path_to_image_directory = "../../img/events/" . urldecode($_GET['name']) . "/";
$path_to_thumbs_directory = "../../img/events/" . urldecode($_GET['name']) . "/thumbnails/";
function createThumbnail($filename)
{
if(preg_match('/[.](jpg)$/', $filename))
{
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
}
elseif(preg_match('/[.](gif)$/', $filename))
{
$im = imagecreatefromgif($path_to_image_directory . $filename);
}
elseif(preg_match('/[.](png)$/', $filename))
{
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
imagejpeg($nm, $path_to_thumbs_directory . $filename);
$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
echo $tn;
}
if(isset($_FILES['fupload'])) {
if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {
$filename = $_FILES['fupload']['name'];
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail($filename);
}
}
?>
Basically it is supposed to generate a thumbnail of the uploaded image and store the original image into a different folder.
The paths are correct, it works by getting the folder name in the URL, it does work, but nothing works for the thumbnails folder.
BEFORE you ask this related question, yes, thumbnails generation does work on my server by the PHP GD, I have tested it separately. So this is not the problem. :)
How do I get this to work? :(
"8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9,"
I'm doing a programming challenge where i need to parse this sequence into my sudoku script.
Need to get the above sequence into 8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8.........
I tried re but without success, help is appreciated, thanks.
Yes/no-question: Is there a Groovy GDK function to capitalize the first character of a string?
I'm looking for a Groovy equivalent of Perl's ucfirst(..) or Apache Commons StringUtils.capitalize(str) (the latter capitalizes the first letter of all words in the input string).
I'm currently coding this by hand using ..
str = str[0].toUpperCase() + str[1 .. str.size() - 1]
.. which works, but I assume there is a more Groovy way to do it. I'd imagine ucfirst(..) being a more common operation than say center(..) which is a standard method in the Groovy GDK (see http://groovy.codehaus.org/groovy-jdk/java/lang/String.html).
I have a string that I would like represented uniquely as an integer.
For example: A3FJEI = 34950140
How would I go about writing a EncodeAsInteger(string) method. I understand that the amount of characters in the string will make the integer increase greatly, forcing the value to become a long, not an int.
Since I need the value to be an integer, I don't need the numerical representation to be entirely unique to the string.
Maybe I can foreach through all the characters of the string and sum the numerical keycode of the character.
First of all, I'm not sure if solution even exists. I spent more than a couple of hours trying to come up with one, so beware.
The problem:
r1 contains an arbitrary integer, flags are not set according to its value. Set r0 to 1 if r1 is 0x80000000, to 0 otherwise, using only two instructions.
It's easy to do that in 3 instructions (there are many ways), however doing it in 2 seems very hard, and may very well be impossible.
Here is a snap of my database.
Both col1 and col2 are declared as int.
My ComputedColumn currently adds the Columns 1 and 2, as follows...
col1 col2 ComputedColumn
1 2 3
4 1 5
Instead of this, my ComputedColumn should join the columns 1 and 2 (includimg the '-' character in the middle) as follows...
col1 col2 ComputedColumn
1 2 1-2
4 1 4-1
So, what is the correct syntax?
How can I filter a string in c? I want to remove anything that isn't [a-z0-9_].
int main(int argc, char ** argv) {
char* name = argv[1];
// remove anything that isn't [a-z0-9_]
printf("%s", name);
}
Mhh, kinda hard to explain with my poor english ;)
So, lets say I have an image, doesnt matter what kind of (gif, jpg, png) with 200x200 pixel size (total area 40000 pixels)
This image have a background, that can be trasparent, or every color (but i know the background-color in advance).
Lets say that in the middle of this image, there is a picture (for keep the example simple lets suppose is a square drawn), of 100x100 pixels (total area 10000 pixels).
I need to know the area percentage that the small square fill inside the image.
So, in i know the full image size and the background-color, there is a way in php/python to scan the image and retrieve that (in short, counting the pixel that are different from the given background)?
In the above example, the result should be 25%
I am trying to pull dynamics from a load that I run using bash. I have gotten to a point where I get the string I want, now from this I want to pull certain information that can vary. The string that gets returned is as follows:
Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0
Each of the number can and will vary in length, but the overall structure will remain the same. What I want to do is be able to get these numbers and load them into some bash variables ie:
RECORDS=??
DELETED=??
SKIPPED=??
WARNING=??
In regex I would do it like this:
Records: (\d*?) Deleted: (\d*?) Skipped (\d*?) Warnings (\d*?)
and use the 4 groups in my variables.
I have a java properties file containing a key/value pair of country names and codes. I will load the contents of this file into a Collection like List or HashMap.
Then, I want users to be able to search for a country, e.g if they type 'Aus' in a textbox and click submit, then I want to search through the collection I have, containing a key/value pair of country codes/names (e.g AUS=Australia), and return those countries which are found matching.
Is there any more efficient way of doing this, other than looping through the elements of the collection and using charAt()?
I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered
Boulder, Co
80303
Boulder, Colorado
Boulder, Co 80303
...
any variation of these it would return the city, state or zip.
This is all going to be user inputted data and inputted in one text field.
How can I list all possible values of a floating-point data type? I can do this using a union in C or C++ but will that be portable?
How can this be done in other languages? Javascript?
Let's just assume I am using this iteration to map theta to sin(theta).
I would like to insert a line break into the first space between words in a string variable. Here is my code so far:
<cfset myPosition = find(" ", #myVar#)>
<cfset lineBreak = Chr(13)&Chr(10)>
<cfset myVar = insert(#lineBreak#, #myVar#, #myPosition#)>
What am I doing wrong?
Hi, I am trying to remove text that is within parentheses (along with the parentheses themselves) but am having trouble with the scenario where there are parentheses within parentheses. This is the method I am using (in Ruby):
sentence.gsub(/\(.*?\)/, "")
and that works fine until I have a sentence such as:
"This is (a test (string))"
Then the above chokes. Anyone have any idea how to do this? I am completely stumped.
Hello there, I have a string which looks like this "a 3e,6s,1d,3g,22r,7c 3g,5r,9c 19.3", how do I go through it and extract the integers and assign them to its corresponding letter variable?. (i have integer variables d,r,e,g,s and c). The first letter in the string represents a function, "3e,6s,1d,3g,22r,7c" and "3g,5r,9c" are two separate containers . And the last decimal value represents a number which needs to be broken down into those variable numbers.
my problem is extracting those integers with the letters after it and assigning them into there corresponding letter. and any number with a negative sign or a space in between the number and the letter is invalid. How on earth do i do this?
Hi,
I am looking for a regular expression for matching that contains no white space in between text but it may or may not have white space at start or end of text.
The problem is: given an integer val1 find the position of the highest bit set (Most Significant Bit) then, given a second integer val2 find a contiguous region of unset bits, with the minimum number of zero bits given by width to the left of the position (ie, in the higher bits).
Here is the C code for my solution:
typedef unsigned int t;
unsigned const t_bits = sizeof(t) * CHAR_BIT;
_Bool test_fit_within_left_of_msb( unsigned width,
t val1,
t val2,
unsigned* offset_result)
{
unsigned offbit = 0;
unsigned msb = 0;
t mask;
t b;
while(val1 >>= 1)
++msb;
while(offbit + width < t_bits - msb)
{
mask = (((t)1 << width) - 1) << (t_bits - width - offbit);
b = val2 & mask;
if (!b)
{
*offset_result = offbit;
return true;
}
if (offbit++) /* this conditional bothers me! */
b <<= offbit - 1;
while(b <<= 1)
offbit++;
}
return false;
}
Aside from faster ways of finding the MSB of the first integer, the commented test for a zero offbit seems a bit extraneous, but necessary to skip the highest bit of type t if it is set.
I have also implemented similar algorithms but working to the right of the MSB of the first number, so they don't require this seemingly extra condition.
How can I get rid of this extra condition, or even, are there far more optimal solutions?
Edit: Some background not strictly required. The offset result is a count of bits from the high bit, not from the low bit as maybe expected. This will be part of a wider algorithm which scans a 2D array for a 2D area of zero bits.
Here, for testing, the algorithm has been simplified. val1 represents the first integer which does not have all bits set found in a row of the 2D array. From this the 2D version would scan down which is what val2 represents.
Here's some output showing success and failure:
t_bits:32
t_high: 10000000000000000000000000000000 ( 2147483648 )
---------
-----------------------------------
*** fit within left of msb test ***
-----------------------------------
val1: 00000000000000000000000010000000 ( 128 )
val2: 01000001000100000000100100001001 ( 1091569929 )
msb: 7
offbit:0 + width: 8 = 8
mask: 11111111000000000000000000000000 ( 4278190080 )
b: 01000001000000000000000000000000 ( 1090519040 )
offbit:8 + width: 8 = 16
mask: 00000000111111110000000000000000 ( 16711680 )
b: 00000000000100000000000000000000 ( 1048576 )
offbit:12 + width: 8 = 20
mask: 00000000000011111111000000000000 ( 1044480 )
b: 00000000000000000000000000000000 ( 0 )
offbit:12
iters:10
***** found room for width:8 at offset: 12 *****
-----------------------------------
*** fit within left of msb test ***
-----------------------------------
val1: 00000000000000000000000001000000 ( 64 )
val2: 00010000000000001000010001000001 ( 268469313 )
msb: 6
offbit:0 + width: 13 = 13
mask: 11111111111110000000000000000000 ( 4294443008 )
b: 00010000000000000000000000000000 ( 268435456 )
offbit:4 + width: 13 = 17
mask: 00001111111111111000000000000000 ( 268402688 )
b: 00000000000000001000000000000000 ( 32768 )
***** mask: 00001111111111111000000000000000 ( 268402688 )
offbit:17
iters:15
***** no room found for width:13 *****
(iters is the count of iterations of the inner while loop)
Well it is a basic question but I seem confused enough.
#include<stdio.h>
int main()
{
char a[100];
printf("Enter a string\n");
scanf("%s",a);
}
Basically the above is what I want to achieve.
If I enter a string
James Bond
then I want that to be stored in array a.
But the problem is because of presence of a blank space in between only James word is stored.
So how can I solve this one.
UPDATE
After the replies given below I understand fgets() would be a better choice. I want to know internal working of fgets as why is it able to store the string with space where as scanf is not able to do the same.
Hello,
I am trying to combine two integers in my application. By combine I mean stick one byte stream at the end of the other, not concatenate the strings.
The two integers are passed from hardware that can't pass a 32 bit value directly, but passes two consecutive 16-bit values separately.
Thanks,
Given two strings with * wildcards, I would like to know if a string could be created that would match both.
For example, these two are a simple case of overlap:
Hello*World
Hel*
But so are all of these:
*.csv
reports*.csv
reportsdump.csv
Is there an algorithm published for doing this? Or perhaps a utility function in Windows or a library I might be able to call or copy?