balance

How to check if a String is balanced?

主宰稳场 提交于 2019-12-17 16:36:17
问题 I want to test if an input String is balanced. It would be balanced if there is a matching opening and closing parenthesis, bracket or brace. example: {} balanced () balanced [] balanced If S is balanced so is (S) If S and T are balanced so is ST public static boolean isBalanced(String in) { Stack st = new Stack(); for(char chr : in.toCharArray()) { if(chr == '{') st.push(chr); } return false; } I'm having problems choosing what to do. Should I put every opening or closing parenthesis,

Check if n-ary tree is balanced in Common Lisp

允我心安 提交于 2019-12-12 04:17:30
问题 I'm trying to write the code to check whether an n-ary tree is balanced or not in clisp. The tree is given like this: (A (B (E (I))(F))(C (G))(D)) which would look like: A / | \ B C D /\ | E F G | I Which would be unbalanced. I was thinking about solving it using something like: the max level of all leaves of a letter - the min level of all leaves shouldnt be bigger than 1. I thought about applying this rule for A,B,C first. If the difference is not bigger than 1, then check for E,F,G until I

Previous Balance added to First Row only in Access Report

本小妞迷上赌 提交于 2019-12-12 04:15:26
问题 I need help on Access Report. As shown in the image attached, I want the "Previous Balance" value (the text box holding (167,950.00)) added to the first row of the new month only (the "Deposit" column). The "Balance" column has Running Sum in the property. As you can see, I'm getting negative values because the previous balance is not added to the first row of new month. Please help. See Sample Report 回答1: Well, I figured it out. I saw this post: 1- is to have the open balance in a text box

How to get the New Running Balance from Existing Balance?

微笑、不失礼 提交于 2019-12-11 09:40:57
问题 Here's my sql and Output of my query... sql: SELECT id ID, token TK, actual_pay PAY, IF(@rtp IS NULL, @rtp:=token, @rtp:=@bal+actual_pay) RTP, IF(@bal IS NULL, @bal:=actual_pay-token, @bal:=@rtp-token) BAL FROM token_table a JOIN (SELECT @rtp:=NULL, @bal:=NULL) b; Output: +----+------+-----+------+------+ | ID | TK | PAY | RTP | BAL | +----+------+-----+------+------+ | 1 | 500 | 900 | 500 | 400 | | 2 | 1200 | 900 | 1300 | 100 | | 3 | 900 | 900 | 1000 | 100 | | 4 | 900 | 900 | 1000 | 100 | |

Algorithm - Attempting to balance out team skill levels, while having a equal amount of players

蹲街弑〆低调 提交于 2019-12-08 17:29:49
I have 3 teams, They have 2 players, 3 players and 7 players. There is 18 players sitting on the sidelines waiting to be assigned. Each player has their own skill level, meaning a level 1 is not going to defeat a level 10. I want to balance the teams out to 10 players each. And I want to try get all 3 teams as equal in skill as I can. But I don't want to remove the players already in a team. But I'm not sure how I would accomplish this. I'm also not sure if there is a easy answer, or if this would be expensive to compute. The skill level is a number which I already have. The teams all have a

SQL query for calculating account balance

放肆的年华 提交于 2019-12-02 13:44:38
I want to calculate account balance using raw sql without extra application logic. Transaction schema includes amount, from_account_id and to_account_id My query is SELECT SUM(tdebit.amount) - SUM(tcredit.amount) as balance FROM accounts as a INNER JOIN transactions as tdebit ON a.id = tdebit.to_account_id INNER JOIN transactions as tcredit ON a.id = tcredit.from_account_id WHERE a.id = $1 AND tdebit.succeed = true AND tcredit.succeed = true And it does not work as I expected - result is wrong, but if I join transaction only once it works correctly, for example just debit amount is ok SELECT

AVL Binary Heap(Balanace test)

笑着哭i 提交于 2019-12-02 06:40:32
I'm trying to achieve a testing if a tree is AVL tree or not using prolog. I've made a height test that works for the tests I've done so far but my balancing test is still not going strong. This is my work so far: avl('Branch'(LeftBranch,RightBranch)) :- height(LeftBranch,H1), height(RightBranch,H2), abs(H1-H2) =< 1. I've based this code from an older stackoverflow code. But it doesn't work in all cases. Will include my height code. Somewhere I've made a misstake and Im sure where to find it. height(leaf(_),1). height('Branch'(LeftBranch,RightBranch,H) :- height(LeftBranch,H1), height

Scala: Make sure braces are balanced

孤者浪人 提交于 2019-12-01 21:36:30
I am running a code to balance brackets in statement. I think i have gotten it correct but it is failing on one particular statement, i need to understand why? This is the test in particular it is failing "())(" More than the coding i think i need to fix the algo, any pointers? def balance(chars: List[Char]): Boolean = { def find(c: Char, l: List[Char], i: Int): Int={ if( l.isEmpty ) { if(c=='(') i+1 else if(c==')') i-1 else i } else if (c=='(') find(l.head, l.tail, i+1) else if(c==')') find(l.head,l.tail, i-1) else find(l.head,l.tail, i) } if(find(chars.head, chars.tail,0) ==0 ) true else

R elegant way to balance unbalanced panel data

一曲冷凌霜 提交于 2019-11-29 14:39:58
问题 Is there an elegant way to balance an unbalanced panel data set? I would like to start with an unbalanced panel (ie, some individuals are missing some data) and end up with a balanced panel (ie, all individuals are missing no data). Below is some sample code. The correct end result is for all observations on 'Frank' and 'Edward' to remain and for all observations on 'Tony' to be removed since he has some missing data. Thank you. unbal <- data.frame(PERSON=c(rep('Frank',5),rep('Tony',5),rep(

Calculate balance with mysql

白昼怎懂夜的黑 提交于 2019-11-28 20:57:36
I have a table which contains the following data: ID In Out 1 100.00 0.00 2 10.00 0.00 3 0.00 70.00 4 5.00 0.00 5 0.00 60.00 6 20.00 0.00 Now I need a query which gives me the following result: ID In Out Balance 1 100.00 0.00 100.00 2 10.00 0.00 110.00 3 0.00 70.00 40.00 4 5.00 0.00 45.00 5 0.00 60.00 -15.00 6 20.00 0.00 5.00 Is it possible to do this with one query, without using a trigger or stored procedures? Short answer, yes Longer answer, you can use a variable to tally it up as it iterates down the rows, i.e. SELECT `table`.`ID`, `table`.`In`, `table`.`Out`, @Balance := @Balance +