jade - using if statement - node.js

I want to get following if condition in jade.
each item,count in display
if(count % 3 === 0)
{
ul.thumbnails
}
li.span6 //nested under ul
//more code nested under li
I googled searched a lot, but with no luck.
Basically, I want to make a new list for every count which is divisible by 3
I tried this:
mixin display_list
li
//code
each item,count in display
-if(count === 0 )
ul.thumbnails
mixin display_list
-else
mixin display_list
It still doesn't work!

Since Jade forces you to indent stuff that is nested, I think the only way (not sure, but it's certainly the most straightforward) is to do it like this:
- var i = 0;
- while(i < display.length)
ul.thumbnails
- var k = i + 3
- while(i < k && i < display.length) // Will repeat three times, unless display.length isn't large enough
li.span6 //nested under ul
//more code nested under li
- i++
Assuming display is an array
(This answer has been updated, the former answer was completely wrong)
Update 2: Fixed that k could be greater than display.length

I found a way but I don't think it is the correct way to do it.
a = [1,2,3,4,54,6,7,8,9,4,5]
each item, i in a
if i % 2 == 0
|<ul>
li #{item}
if (i+1) % 2 == 0
|</ul>

Related

Invalid get index 'x' candy crush game on Godot

I am having a problem with i think is the index. i can't run the code, somehow the "while" is wrong and i don't know how. I am coping with a youtube video how to build it. but i think the video is outdated, is from 3 years ago. if somone get a ideia how to run it i would be grateful. (sorry bad English, not my formal language)
some phrases will be in portuguese but i think is not gonna damege it...
edit: i think is not a error. but its a red phrase is on the scrubber area
extends Node2D
# Grid Variaveis
export (int) var width;
export (int) var height;
export (int) var x_start;
export (int) var y_start;
export (int) var offset;
var possible_pieces = [
preload("res://cenario/abacate.tscn"),
preload("res://cenario/sprite2.tscn"),
preload("res://cenario/Node2D.tscn"),
preload("res://cenario/sprite 3.tscn"),
preload("res://cenario/tomato.tscn"),
preload("res://cenario/uva.tscn")
];
var all_pieces =[];
func _ready():
randomize();
all_pieces = make_2d_array();
spawn_pieces();
func make_2d_array():
var array = [];
for i in width:
array.append([]);
for j in height:
array[i].append(null)
return array;
func spawn_pieces():
for i in width:
for j in height:
#choose a random number and store it
var rand = floor(rand_range(0, possible_pieces.size()));
var piece = possible_pieces[rand].instance();
var loops = 0
while(match_at(i, j, piece[rand].color) && loops < 100):
rand = floor(rand_range(0, possible_pieces.size()));
loops += 1;
piece = possible_pieces[rand].instance();
#instance that piece from the array
add_child(piece);
piece.position = grid_to_pixel(i, j);
all_pieces[i][j] = piece;
func match_at(i, j, color):
if i > 1:
if all_pieces[i - 1][j] != null && all_pieces[i - 2][j] != null:
if all_pieces[i - 1][j].color == color && all_pieces[i - 2][j].color == color:
return true;
if j > 1:
if all_pieces[i][j - 1] != null && all_pieces[i][j - 2] != null:
if all_pieces[i][j - 1].color == color && all_pieces[i][j - 2].color == color:
return true;
pass;
func grid_to_pixel(column, row):
var new_x = x_start + offset * column;
var new_y = y_start + -offset * row;
return Vector2(new_x, new_y);
You have possible_pieces with a bunch of PackedScenes:
var possible_pieces = [
preload("res://cenario/abacate.tscn"),
preload("res://cenario/sprite2.tscn"),
preload("res://cenario/Node2D.tscn"),
preload("res://cenario/sprite 3.tscn"),
preload("res://cenario/tomato.tscn"),
preload("res://cenario/uva.tscn")
];
Then you pick one and instantiate it:
var rand = floor(rand_range(0, possible_pieces.size()));
var piece = possible_pieces[rand].instance();
Thus piece is a Node. So when you do this: piece[rand] you are trying to use index access on a Node and that does not work. That is the problem with this line:
while(match_at(i, j, piece[rand].color) && loops < 100):
I believe you want to do this instead:
while(match_at(i, j, piece.color) && loops < 100):
For future reference, specifying the types of your variables might help you avoid this kind of errors. See Static typing in GDScript. By the way, even though the term "Type Hints" stuck, they are not hints, they are actual type declarations.
The function match_atreturns null if it doesn't match any of the conditions.
Replace the pass; for return false; or create an else statement to do something if needed, and then the return false;.
As mentioned below by #Theraot i realized the pass; its not the problem

I'm looking for a particular insertion algorithm but I can't think of the name

Basically I have a method that will take any set of numbers with a selected value and reorder them ascending keeping track of the new selected value.
So {21, 2, 9, 13} selectedVal = 9 becomes { 1, 2, 3, 4 } selectedVal = 2
What I need to do now is be able to swap 2 numbers and keep the order intact. If I want to swap 2 with 4 I need 3->2 and 4->3 but also the other way around like swapping 3 with 1 so 1->2 and 2->3.
I'm sure there is a proven algorithm for this and I will probably create my own for fun tomorrow but I'd like the fastest solution to use in my actual application.
The name of the algorithm or any useful links would be appreciated. I am using C# 4.0 if that matters at all.
EDIT - I came up with my own solution that I believe works on paper but I'd still like a reply if someone knows a better solution.
Assuming ordered list always starts at 1 which it does in my case...
if (newpos < currpos)
for (int i = newpos-1; i < currpos-1; i++)
{
array[i] += 1;
array[currpos-1] = newpos;
}
else if (newpos > currpos)
for (int i = newpos-1; i >= currpos-1; i--)
{
array[i] -= 1;
array[currpos-1] = newpos;
}
else // do nothing since they are trying to swap to the same place

Node.js variables being changed without any operations performed on them

I am using a node.js server for a multiplayer synchronized dice, but i am having some strange problems with variables changing that are not referenced or used...
var origonalRolls = rolls;
//identify epic fails
var epicFails = [];
for(var r = 0; r < rolls.length; r++)
if(rolls[r] === 1)
epicFails.push(r);
console.log("TEST 1 :: " + JSON.stringify(rolls));
console.log("TEST 2 :: " + JSON.stringify(origonalRolls));
//remove epic fails and the corresponding heighest
if(epicFails.length > 0){
for(var i = epicFails.length-1; i >= 0; i--){
rolls.splice(epicFails[i], 1);
if(rolls[rolls.length-1] >= success)
rolls.splice(rolls.length-1, 1);
}
}
console.log("TEST 3 :: " + JSON.stringify(rolls));
console.log("TEST 4 :: " + JSON.stringify(origonalRolls));
the above should find any element in the rolls array which is 1 and then add it to epicFails. it should then remove it from rolls as well as the heighest remaining roll. (note, rolls is sorted numerically)
for some reason the output of this segment of code is as follows:
TEST 1 :: [1,1,2,3,3,6,7,7,9,9]
TEST 2 :: [1,1,2,3,3,6,7,7,9,9]
TEST 3 :: [2,3,3,6,7,7]
TEST 4 :: [2,3,3,6,7,7]
I am unsure why rolls and origonalRolls start the same and end the same. I am only using rolls.
Any help and/or explanation to this problem is welcome, it's been troubling me for a long time now...
In Javascript Arrays and Objects are only shallow copied - which means that an array (rolls) copied from another array (originalRolls) is only a reference to originalRolls - it is not an entirely new array, and modifying values in one will affect values in the other.
You will need to implement a deep copy function to create an entirely new array based off another. There are numerous imlementations of deep copying arrays/objects both here and elsewhere on the net - here is one of them from a quick Google.
Replace var origonalRolls = rolls; with:
var origonalRolls = [];
for (var i = 0, len = rolls.length; i < len; i++) {
origonalRolls[i] = rolls[i];
}

Make Rows with large data set in Jade Templating in Node.js

I have about 100 items that I use jade iteration to write each one of them to html. However, I'm struggling to find an elegant way to separate the items into rows of three.
To be clear, I want something like this:
.row
.item1
.item2
.item3
.row
.item1
... and so on...
I've tried things with inline javascript like this with no luck:
- var a = 0;
each item in list
- a++;
- if(a % 3 == 0)
.row-fluid
.span3(id='#{item.id}')
p #{item.id}
- else
.span3(id='#{item.id}')
p #{item.id}
note: this kills the list
while list.length > 0
.row
for item in list.splice(0, 3)
.span3(id=item.id)
p= item.id
let me know if this works as i just wrote it off the top of my head
- var i = -3;
while i <= list.length
- i += 3;
.row
each item in list.slice(i, i+3)
.span3(id='#{item.id}')

Is it possible to do a Levenshtein distance in Excel without having to resort to Macros?

Let me explain.
I have to do some fuzzy matching for a company, so ATM I use a levenshtein distance calculator, and then calculate the percentage of similarity between the two terms. If the terms are more than 80% similar, Fuzzymatch returns "TRUE".
My problem is that I'm on an internship, and leaving soon. The people who will continue doing this do not know how to use excel with macros, and want me to implement what I did as best I can.
So my question is : however inefficient the function may be, is there ANY way to make a standard function in Excel that will calculate what I did before, without resorting to macros ?
Thanks.
If you came about this googling something like
levenshtein distance google sheets
I threw this together, with the code comment from milot-midia on this gist (https://gist.github.com/andrei-m/982927 - code under MIT license)
From Sheets in the header menu, Tools -> Script Editor
Name the project
The name of the function (not the project) will let you use the func
Paste the following code
function Levenshtein(a, b) {
if(a.length == 0) return b.length;
if(b.length == 0) return a.length;
// swap to save some memory O(min(a,b)) instead of O(a)
if(a.length > b.length) {
var tmp = a;
a = b;
b = tmp;
}
var row = [];
// init the row
for(var i = 0; i <= a.length; i++){
row[i] = i;
}
// fill in the rest
for(var i = 1; i <= b.length; i++){
var prev = i;
for(var j = 1; j <= a.length; j++){
var val;
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
row[j - 1] = prev;
prev = val;
}
row[a.length] = prev;
}
return row[a.length];
}
You should be able to run it from a spreadsheet with
=Levenshtein(cell_1,cell_2)
While it can't be done in a single formula for any reasonably-sized strings, you can use formulas alone to compute the Levenshtein Distance between strings using a worksheet.
Here is an example that can handle strings up to 15 characters, it could be easily expanded for more:
https://docs.google.com/spreadsheet/ccc?key=0AkZy12yffb5YdFNybkNJaE5hTG9VYkNpdW5ZOWowSFE&usp=sharing
This isn't practical for anything other than ad-hoc comparisons, but it does do a decent job of showing how the algorithm works.
looking at the previous answers to calculating Levenshtein distance, I think it would be impossible to create it as a formula.
Take a look at the code here
Actually, I think I just found a workaround. I was adding it in the wrong part of the code...
Adding this line
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
so it now reads
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
Seems to fix the problem. Now 'biulding' is 92% accurate and 'bilding' is 88%. (whereas with the original formula 'biulding' was only 75%... despite being closer to the correct spelling of building)

Resources