taking the mirror image of a int array in java
Category - Java
Here i will show all the methods that can be used in taking the mirror image of a int array
1st method – using a stack.
Here the array is inserted into a stack and then the array is overwritten with the popped value from stack.
static void arrayMirror(int array[]){
Stack<Integer> stack = new Stack<Integer>();
for(int i =0; i < array.length; i++){
stack.push(array[i]);
}
for(int i = 0 ; i < array.length ; i++){
array[i] = stack.pop();
}
}
2nd method using swap
Here we swap i with array.length – i position with i value incremented until we reach the middle of the array.
static void arrayMirror(int array[]){
int temp = 0;
int arrayLength = array.length;
for(int i =0 ; i < array.length /2 ; i++){
temp = array[i];
array[i] = array[arrayLength -1 - i];
array[arrayLength -1 - i] = temp;
}
}
Convert folder containing bitmap images to eps format
Category - Windows
1. Make sure you have imagemagick in your system. If not then you can download it from their siteĀ http://www.imagemagick.org/script/index.php
2. set the path to the installation directory. This is where the convert program is located
3. Open windows command prompt
4. go to the directory containing the bitmap files
5. then enter the following command
for %f in (*.bmp) do convert -compress none %f eps2:%~nf.eps
Duplicate file remover in C#
Category - C#
This snippets takes the sha1 of each file in the specified folder and deletes files having similar sha1 values.
Be cautious when running this as the deleted files will not go to the recycle bin.
static void deleteDuplicates(string folderName)
{
Hashtable hashtable = new Hashtable();
string[] filelist = FileIO.fileList(folderName);
for (int j = 0; j < filelist.Length; j++)
{
string s = GetSha1HashFromFile(filelist[j]);
Boolean value = hashtable.ContainsKey(s);
if (value == true)
{
object val = hashtable[s];
Console.WriteLine("\n\n" + filelist[j] + " \n " + val.ToString() + " " + s);
if (!TryToDelete(filelist[j].Replace(@"\", @"\\")))
{
// Do some action if file couldnt be deleted
}
}
else
{
hashtable.Add(s, filelist[j]);
}
}
}
static bool TryToDelete(string f)
{
try
{
// A.
// Try to delete the file.
File.Delete(f);
return true;
}
catch (IOException)
{
// B.
// couldn't delete the file.
return false;
}
}
public static string GetSha1HashFromFile(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] retVal = sha1.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString().Trim();
}
}
