def get_solved_unsolved(): if request.extension != "json" or \ request.vars.user_id is None or \ request.vars.custom is None: raise HTTP(400, "Bad request") return user_id = int(request.vars.user_id) custom = (request.vars.custom == "True") solved_problems, unsolved_problems = utilities.get_solved_problems( user_id, custom) if auth.is_logged_in() and session.user_id == user_id and not custom: user_solved_problems, user_unsolved_problems = solved_problems, unsolved_problems else: if auth.is_logged_in(): user_solved_problems, user_unsolved_problems = utilities.get_solved_problems( session.user_id, False) else: user_solved_problems, user_unsolved_problems = set([]), set([]) category_wise_solved_problems, category_wise_unsolved_problems = \ utilities.get_category_wise_problems(solved_problems, unsolved_problems, user_solved_problems, user_unsolved_problems) return dict(solved_problems=category_wise_solved_problems, unsolved_problems=category_wise_unsolved_problems, solved_html_widget=str( utilities.problem_widget("", "", "solved-problem", "Solved problem", None)), unsolved_html_widget=str( utilities.problem_widget("", "", "unsolved-problem", "Unsolved problem", None)), unattempted_html_widget=str( utilities.problem_widget("", "", "unattempted-problem", "Unattempted problem", None)))
def generate_recommendations(user_id): """ Generate recommendations based on past solved problems. @param user_id (Integer): User ID for which recommendations should be generated. @return (List): Problem IDs of the new recommendations. """ db = current.db ptable = db.problem solved_problems, unsolved_problems = utilities.get_solved_problems( user_id, False) query = (~ptable.id.belongs(solved_problems)) & \ (~ptable.id.belongs(unsolved_problems)) rows = db(query).select(ptable.id, orderby='<random>', limitby=(0, stopstalk_constants.RECOMMENDATION_COUNT)) return [x.id for x in rows]
def get_solved_unsolved(): if request.extension != "json" or \ request.vars.user_id is None or \ request.vars.custom is None: raise HTTP(400, "Bad request") return user_id = int(request.vars.user_id) custom = (request.vars.custom == "True") solved_problems, unsolved_problems = utilities.get_solved_problems( user_id, custom) if auth.is_logged_in() and session.user_id == user_id and not custom: user_solved_problems, user_unsolved_problems = solved_problems, unsolved_problems else: if auth.is_logged_in(): user_solved_problems, user_unsolved_problems = utilities.get_solved_problems( session.user_id, False) else: user_solved_problems, user_unsolved_problems = set([]), set([]) solved_ids, unsolved_ids = [], [] ptable = db.problem sttable = db.suggested_tags ttable = db.tag all_tags = db(ttable).select() all_tags = dict([(tag.id, tag.value) for tag in all_tags]) query = ptable.id.belongs(solved_problems.union(unsolved_problems)) # id => [problem_link, problem_name, problem_class] # problem_class => # 0 (Logged in user has solved the problem) # 1 (Logged in user has attemted the problem) # 2 (User not logged in or not attempted the problem) problem_details = {} pids = [] for problem in db(query).select(ptable.id, ptable.link, ptable.name): pids.append(problem.id) problem_status = 2 if problem.id in user_unsolved_problems: # Checking for unsolved first because most of the problem links # would be found here instead of a failed lookup in solved_problems problem_status = 1 elif problem.id in user_solved_problems: problem_status = 0 problem_details[problem.id] = [ problem.link, problem.name, problem_status, problem.id ] if problem.id in solved_problems: solved_ids.append(problem.id) else: unsolved_ids.append(problem.id) problem_tags = {} query = (sttable.problem_id.belongs(pids)) & \ (sttable.user_id == 1) for prow in db(query).select(sttable.tag_id, sttable.problem_id): if prow.problem_id not in problem_tags: problem_tags[prow.problem_id] = set([]) problem_tags[prow.problem_id].add(prow.tag_id) categories = { "Dynamic Programming": set([1]), "Greedy": set([28]), "Strings": set([20]), "Hashing": set([32]), "Bit Manipulation": set([21, 42]), "Trees": set([6, 9, 10, 11, 17, 31]), "Graphs": set([4, 5, 15, 22, 23, 24, 26]), "Algorithms": set([12, 14, 27, 29, 35, 36, 37, 38, 44, 51]), "Data Structures": set([2, 3, 7, 8, 33, 34, 49]), "Math": set([16, 30, 39, 40, 41, 43, 45, 50, 54]), "Implementation": set([13, 18, 19]), "Miscellaneous": set([46, 47, 48, 52]) } ordered_categories = [ "Dynamic Programming", "Greedy", "Strings", "Hashing", "Bit Manipulation", "Trees", "Graphs", "Algorithms", "Data Structures", "Math", "Implementation", "Miscellaneous" ] displayed_problems = set([]) def _get_categorized_json(problem_ids): result = dict([(category, []) for category in ordered_categories]) for pid in problem_ids: this_category = None if pid not in problem_tags: this_category = "Miscellaneous" else: ptags = problem_tags[pid] category_found = False for category in ordered_categories: if len(categories[category].intersection(ptags)) > 0: this_category = category category_found = True break if not category_found: this_category = "Miscellaneous" pdetails = problem_details[pid] plink, pname, _, _ = pdetails psite = utilities.urltosite(plink) if (pname, psite) not in displayed_problems: displayed_problems.add((pname, psite)) result[this_category].append(problem_details[pid]) return result return dict(solved_problems=_get_categorized_json(solved_ids), unsolved_problems=_get_categorized_json(unsolved_ids), solved_html_widget=str( utilities.problem_widget("", "", "solved-problem", "Solved problem", None)), unsolved_html_widget=str( utilities.problem_widget("", "", "unsolved-problem", "Unsolved problem", None)), unattempted_html_widget=str( utilities.problem_widget("", "", "unattempted-problem", "Unattempted problem", None)))
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import utilities stable = db.submission atable = db.auth_user BATCH_SIZE = 100 attempted_list = [] solved_list = [] accuracy_list = [] last_id = db(atable).select(orderby=~atable.id).first().id for i in xrange(1, last_id): solved, unsolved = utilities.get_solved_problems(i) solved = len(solved) unsolved = len(unsolved) submissions = db(stable.user_id == i).select(stable.status) submissions = [x.status for x in submissions] try: accuracy = submissions.count("AC") * 100.0 / len(submissions) except: accuracy = 0 attempted_list.append(unsolved) solved_list.append(solved) accuracy_list.append(accuracy) print i, solved, unsolved, accuracy print "_______________________________" print attempted_list
def search(): """ Search page for problems """ ttable = db.tag uetable = db.user_editorials problem_name = request.vars.get("name", None) orderby = request.vars.get("orderby", None) clubbed_tags = request.vars.get("generalized_tags", None) q = request.vars.get("q", None) sites = request.vars.get("site", None) include_editorials = request.vars.get("include_editorials", "") exclude_solved = request.vars.get("exclude_solved", None) generalized_tags = db(ttable).select(ttable.value, orderby=ttable.value) generalized_tags = [x.value for x in generalized_tags] if any([problem_name, orderby, clubbed_tags, q, sites]) is False: if request.extension == "json": return dict(total_pages=0) else: if len(request.get_vars): # No filter is applied response.flash = "No filter is applied" return dict(table=DIV(), generalized_tags=generalized_tags) clubbed_tags = None if clubbed_tags == "" else clubbed_tags try: if sites == None or sites == "": sites = [] elif isinstance(sites, str): sites = [sites] except: sites = [] if orderby not in ("accuracy-asc", "accuracy-desc", "solved-count-asc", "solved-count-desc"): orderby = None try: curr_page = int(request.vars["page"]) except: curr_page = 1 PER_PAGE = current.PER_PAGE ptable = db.problem query = True rows = db(uetable.verification == "accepted").select(uetable.problem_id) problem_with_user_editorials = set([x["problem_id"] for x in rows]) if q is not None and not clubbed_tags: # Enables multiple space seperated tag search q = q.split(" ") for tag in q: if tag == "": continue # Decision to make & or | # & => Search for problem containing all these tags # | => Search for problem containing one of the tags query &= ptable.tags.contains(tag) elif clubbed_tags: clubbed_tags = [clubbed_tags] if isinstance(clubbed_tags, str) else clubbed_tags ttable = db.tag sttable = db.suggested_tags tag_ids = db(ttable.value.belongs(clubbed_tags)).select(ttable.id) tag_ids = [x.id for x in tag_ids] problem_ids = db(sttable.tag_id.belongs(tag_ids)).select(sttable.problem_id) problem_ids = [x.problem_id for x in problem_ids] query &= ptable.id.belongs(problem_ids) if problem_name: query &= ptable.name.contains(problem_name) if include_editorials: # Check if the site editorial link is present or the problem id exists # in user_editorials table with accepted status query &= (((ptable.editorial_link != None) & \ (ptable.editorial_link != "")) | \ (ptable.id.belongs(problem_with_user_editorials))) if exclude_solved and auth.is_logged_in(): solved_pids, _ = utilities.get_solved_problems(session.user_id, False) query &= ~ptable.id.belongs(solved_pids) elif exclude_solved and request.extension == "html": response.flash = T("Login to apply this filter") site_query = None for site in sites: if site_query is not None: site_query |= ptable.link.contains(current.SITES[site]) else: site_query = ptable.link.contains(current.SITES[site]) if site_query: query &= site_query accuracy_column = (ptable.solved_submissions * 1.0 / ptable.total_submissions) kwargs = dict(distinct=True, limitby=((curr_page - 1) * PER_PAGE, curr_page * PER_PAGE)) if orderby and orderby.__contains__("accuracy"): query &= ~(ptable.link.contains("hackerrank.com")) kwargs["orderby"] = ~accuracy_column if orderby == "accuracy-desc" else accuracy_column if orderby and orderby.__contains__("solved-count"): kwargs["reverse"] = True if orderby == "solved-count-desc" else False query &= (ptable.solved_submissions != None) query &= (ptable.total_submissions != None) & (ptable.total_submissions != 0) query &= (ptable.user_ids != None) query &= (ptable.custom_user_ids != None) if request.extension == "json": total_problems = db(query).count() total_pages = total_problems / PER_PAGE if total_problems % PER_PAGE != 0: total_pages = total_problems / PER_PAGE + 1 return dict(total_pages=total_pages) if orderby and orderby.__contains__("solved-count"): all_problems = db(query).select().as_list() all_problems.sort(key=lambda x: x["user_count"] + \ x["custom_user_count"], reverse=kwargs["reverse"]) all_problems = all_problems[kwargs["limitby"][0]:kwargs["limitby"][1]] else: # No need of caching here all_problems = db(query).select(**kwargs) return dict(table=utilities.get_problems_table(all_problems, session.user_id, problem_with_user_editorials), generalized_tags=generalized_tags)
import requests from prettytable import PrettyTable from bs4 import BeautifulSoup stopstalk_handle = "lebron" custom = False atable = db.auth_user category_counts_dict = {} user_record = db(atable.stopstalk_handle == stopstalk_handle).select().first() user_id = user_record.id # ============================================================================= solved_problems, unsolved_problems = utilities.get_solved_problems( user_id, custom) solved_category_counts, unsolved_category_counts = utilities.get_category_wise_problems( solved_problems, unsolved_problems, set(), set()) problem_counts_table = PrettyTable() problem_counts_table.field_names = ["Field", "Count"] problem_counts_table.add_row(["Solved problem count", len(solved_problems)]) problem_counts_table.add_row( ["Unsolved problem count", len(unsolved_problems)]) problem_counts_table.add_row( ["Total problem count", len(solved_problems) + len(unsolved_problems)]) print problem_counts_table.get_string()